<script type="text/javascript"> window.onload=function(){ var box1= document.getElementById("box1"); box1.onmouseover=function(){ mvfn1(); } }; var timer = null ; function mvfn1(){ clearInterval(timer); var box1= document.getElementById("box1"); var timer=setInterval(function(){ if (box1.offsetLeft>=0) { clearInterval(timer); } else{ box1.style.left=box1.offsetLeft+1+"px"; } },30); }; </script>
这样你就把全局变量变成局部变量了