請幫忙看看 為什么物體運(yùn)動停不下來
<!DOCTYPE??HTML> <html?> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <title>js運(yùn)動</title> <style> *{margin:0px;padding:0px;} #div1{ ??????height:200px; ?????width:200px; ?????background-color:red; ?????position:relative; ?????left:-200px; ?????} #share{ ??????? ???????height:50px; ??????background-color:blue; ??????position:absolute; ??????top:100px; ??????left:200px; ??????color:#FFFFFF; } </style> <script> ???var?timer=null; ??window.onload=function(){ ?????var?oDiv=document.getElementById('div1'); ?????oDiv.onmouseover=startMove; ??} ???function?startMove(){ ??????clearInterval(timer); ??????var?oDiv=document.getElemenstById('div1'); ??????timer=setInterval(function(){ ?????????if?((oDiv.style.left?=?oDiv.offsetLeft))?{ ????????????clearInterval(timer); ?????????}?else?{ ????????????oDiv.style.left?=?oDiv.offsetLeft?+?1?+?'px'; ?????????} ??????},30); ???} </script> </head> <body> ??<div?id="div1"> ??<span?id="share">分享</span> ?</div> </body> </html>
? 請幫忙看為什么停不下來???
2016-04-15
function startMove(){
? ?clearInterval(timer);
? ?var oDiv=document.getElementById("div1");
? ?timer=setInterval(function(){
? ? ? ?if (oDiv.offsetLeft == 0) {
? ? ? ? ? ?clearInterval(timer);
? ? ? ?} else {
? ? ? ? ? ?oDiv.style.left = oDiv.offsetLeft + 1 + 'px';
? ? ? ?}
? ?},30);
}
你的清楚定時器的判斷錯了!
應(yīng)該是oDiv.offsetLeft == 0?
而不是oDiv.style.left?=?oDiv.offsetLeft