為什么我的timer設(shè)置在function move()之內(nèi)方塊就會來來回回的,但是設(shè)置成全局變量就沒事了呢?下面是我的代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{margin: 0;
padding: 0}
#div{
position:relative;
left: -200px;
width: 200px;
height: 100px;
margin:10px 0;
background: red;
}
#div span{
width: 20px;
height: 50px;
background:blue;
position: absolute;
left: 200px;
top:75px;
}
</style>
<script>
window.onload=function(){
var div=document.getElementById('div');
div.onmouseover=function(){
move(0);
}
div.onmouseout=function(){
move(-200);
}
}
var timer=null;
function move(iTarget){
clearInterval(timer);
var div=document.getElementById('div');
timer=setInterval(function(){
if(div.offsetLeft==iTarget){
clearInterval(timer);
}else{
if(div.offsetLeft<iTarget){
div.style.left=div.offsetLeft+50+'px';
}else if(div.offsetLeft>iTarget){
div.style.left=div.offsetLeft-50+'px';
}
}
},100);
}
</script>
</head>
<body>
<div id="div">
<span id="span">分享</span>
</div>
</body>
</html>
2016-11-15
你現(xiàn)在理解 沒有? 我也在想這個問題。 我們真是用心在學(xué)習(xí)啊,哈哈
2016-10-07
應(yīng)該是變量作用域問題吧