定時器前面一旦加了var 為什么就會抖動
<!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
你在定時器前面重新定義了“int”變量,所以一開始你在程序中調用的清除定時器根本沒發揮作用,函數的清除參數也根本不是你在下方調用的定時器的返回值,而且你在定時器里面的函數里的條件判斷中的“clearInterval()”,根本不對,沒有參數,導致整個程序出錯(ps:定義變量的時候不要用語言自帶的關鍵字“int”,這是硬性的語法規則?。?br />
2016-12-20
為什么把int 前的var去掉就好了