<script> ? ?window.onload=function () ? ?{ ? ? ? ?var odiv=document.getElementById('div1'); ? ? ? ?odiv.onmouseover=function () ? ? ? ?{ ? ? ? ? ? ?StarMove(0); ? ? ? ?} ? ? ? ?odiv.onmouseout=function () ? ? ? ?{ ? ? ? ? ? ?StarMove(-100); ? ? ? ?} ? ?}//var timer=null; ?timer為什么一定要放在這里,而不能放在starmove函數里面? ?function StarMove(iTarget) ? ?{ ? ? ? ?var odiv=document.getElementById('div1'); ? ? ? ?var timer=null; //放這里的運行結果有問題 ? ? ? ?clearInterval(timer); ? ? ? ?timer=setInterval(function (){ ? ? ? ? ? ?var iSpeed=0; ? ? ? ? ? ?if(odiv.offsetLeft<iTarget) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?iSpeed=10; ? ? ? ? ? ?} ? ? ? ? ? ?else ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?iSpeed=-10; ? ? ? ? ? ?} ? ? ? ? ? ?if(odiv.offsetLeft==iTarget) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?clearInterval(timer); ? ? ? ? ? ?} ? ? ? ? ? ?else ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?odiv.style.left=odiv.offsetLeft+iSpeed+'px'; ? ? ? ? ? ?} ? ? ? ?}, 30); ? ?}
麻煩大神解釋下以下代碼中var timer=null為什么不能放在函數里面。
慕田峪8701529
2017-08-22 09:26:48