求js動(dòng)畫效果里面的一些bug解決方法?。?!
<!DOCTYPE?html> <html> <head> ??<meta?charset="UTF-8"> ??<title>mouseOverOut</title> ??<style?type="text/css"> ????*{margin:0;padding:0;} ????#div1{ ??????height:200px; ??????width:200px; ??????background:red; ??????position:relative; ??????left:-200px; ??????top:0; ????} ????#div2{ ??????height:50px; ??????width:20px; ??????background:blue; ??????position:absolute; ??????top:75px; ??????left:200px; ????} ??</style> </head> <body> <script?type="text/javascript"> window.onload=function(){ ??var?oDiv=document.getElementById('div1'); ??oDiv.onmouseover=function(){ ????startMove(0); ??} ??oDiv.onmouseout=function(){ ????startMove(-200); ??} } var?timer=null; var?speed=0; function?startMove(iTarget){ ??clearInterval(timer); ??var?oDiv=document.getElementById('div1'); ??speed=(iTarget-oDiv.offsetLeft)/20; ??speed=speed>0?Math.ceil(speed):Math.floor(speed); ??timer=setInterval(function(){ ????if(oDiv.offsetLeft==iTarget){ ??????clearInterval(timer); ????}else{ ????????oDiv.style.left=oDiv.offsetLeft+speed+'px'; ????} ??},20); } </script> <div?id="div1"> <div?id="div2">分享</div> </div> </body> </html>
以上我是敲得代碼,幾乎和老師的一樣
但是,我測(cè)試過(guò)程有一個(gè)bug,就是在藍(lán)色分享不斷地將鼠標(biāo)移進(jìn)移出就會(huì)出現(xiàn)一些bug,大神們可以測(cè)試一下,幫我解答一下。謝謝啦
2016-09-11
問(wèn)題的起因是由于反復(fù)的進(jìn)行鼠標(biāo)移入移出,導(dǎo)致oDiv.offsetLeft+speed的值超過(guò)iTarget這一邊界,從而不能清除定時(shí)器。
2016-09-12
。。。搞笑,,,,逗我?我初學(xué)者。。。
2016-09-12
為什么我feel不到bug?
2016-09-11
測(cè)試過(guò)了,果然可以了,謝謝你啦