定時(shí)器前面一旦加了var 為什么就會(huì)抖動(dòng)
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>下拉菜單</title>
<style type="text/css">
*{margin:0;padding: 0;font-size: 13px;}
div{height:300px;width:200px;position: relative;left:-200px;top:0;background: red;}
span{width:20px;height:50px;background: blue;position: absolute;left:200px;top:75px;}
</style>
<script>
window.onload=function(){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function(){
startMove();
}
oDiv.onmouseout=function(){
startMove1();
}
}
var int=null;
function startMove(){
clearInterval(int);
var oDiv=document.getElementById('div1');
var int=setInterval(function(){if(oDiv.offsetLeft==0)
{
clearInterval;
}
else
{
oDiv.style.left=oDiv.offsetLeft+1+'px';
}},30);
}
function startMove1(){
clearInterval(int);
var oDiv=document.getElementById('div1');
var int=setInterval(function(){if(oDiv.offsetLeft==-200)
{
clearInterval;
}
else
{
oDiv.style.left=oDiv.offsetLeft-1+'px';
}},30);
}
</script>
</head>
<body>
<div id="div1"><span id="share">分享</span></div>
</body>
</html>
2017-01-19
你在定時(shí)器前面重新定義了“int”變量,所以一開(kāi)始你在程序中調(diào)用的清除定時(shí)器根本沒(méi)發(fā)揮作用,函數(shù)的清除參數(shù)也根本不是你在下方調(diào)用的定時(shí)器的返回值,而且你在定時(shí)器里面的函數(shù)里的條件判斷中的“clearInterval()”,根本不對(duì),沒(méi)有參數(shù),導(dǎo)致整個(gè)程序出錯(cuò)(ps:定義變量的時(shí)候不要用語(yǔ)言自帶的關(guān)鍵字“int”,這是硬性的語(yǔ)法規(guī)則?。?br />
2016-12-20
為什么把int 前的var去掉就好了