Team:UCLA/anuved verma ucla2014 workspace
From 2014.igem.org
(Difference between revisions)
Anuvedverma (Talk | contribs) |
Anuvedverma (Talk | contribs) |
||
Line 14: | Line 14: | ||
function addEvent(obj,evt,fn,capture){ | function addEvent(obj,evt,fn,capture){ | ||
evt=evt.replace('on',''); | evt=evt.replace('on',''); | ||
- | if(obj.attachEvent) | + | if(obj.attachEvent) obj.attachEvent('on'+ evt,fn); |
- | + | ||
- | + | ||
else{ | else{ | ||
- | if(!capture) | + | if(!capture) |
capture=false; | capture=false; | ||
- | + | obj.addEventListener(evt,fn,capture); | |
- | + | ||
} | } | ||
} | } | ||
- | function moveDiv(obj, distance, speed){ | + | function moveDiv(obj,distance,speed){ |
- | if(!distance) | + | if(!distance) |
- | if(!speed) | + | distance=250; |
- | + | if(!speed) | |
- | if(!obj. | + | speed=500; |
- | + | if(!obj.style.marginLeft) | |
- | var timer = null, | + | obj.style.marginLeft=0+'px'; |
- | startTime = (new Date).getTime(), | + | var timer=null, |
- | elapsed = null, | + | startTime=(new Date).getTime(), |
- | d = null | + | elapsed=null, |
- | + | d=null, | |
- | + | dir='right', | |
- | timer = setInterval(function(){ | + | current_distance=obj.style.marginLeft.replace('px',''); |
- | elapsed = (new Date).getTime - startTime; | + | if(distance<current_distance) |
- | if(elapsed < speed){ | + | dir='left'; |
- | / | + | timer=setInterval(function(){ |
- | d = elapsed/speed*distance; | + | elapsed=(new Date).getTime()- startTime; |
- | + | if(elapsed<speed){ | |
+ | if(dir=='right'){ | ||
+ | d=elapsed/speed*distance; | ||
+ | obj.style.marginLeft=d+'px'; | ||
+ | } | ||
+ | else{ | ||
+ | d=elapsed/speed*distance; | ||
+ | d=current_distance- d; | ||
+ | obj.style.marginLeft=d+'px'; | ||
+ | } | ||
} | } | ||
else{ | else{ | ||
- | |||
clearInterval(timer); | clearInterval(timer); | ||
- | obj.style.marginLeft = distance + 'px'; | + | obj.style.marginLeft=distance+'px'; |
} | } | ||
- | }, 5); | + | },5); |
- | + | ||
} | } | ||
var box = document.getElementById('box'); | var box = document.getElementById('box'); |
Revision as of 20:59, 11 July 2014