为什么我的代码中的move()函数和remove()函数中的用来清除上一个定时器的clearInterval(timer)不管用呢?

来源:2-1 JS速度动画

慕粉3244739

2016-05-25 16:12

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<style>

*{

margin: 0;

padding: 0

}

#div{

width: 300px;

height: 200px;

background: red;

position: relative; 

left: -300px;

}

span{

display: block;

width: 20px;

height: 40px;

background: blue;

position: relative; 

left: 300px;

top: 90px;

writing-mode:tb-rl;

text-align: center;

}


</style>

<script>

window.onload=function(){

var div=document.getElementById('div'),

timer=null;

div.onmouseover=function(){

move();

}

div.onmouseout=function(){

remove();

}

function move(){

clearInterval(timer);

temer=setInterval(function(){

if(div.offsetLeft==0){

clearInterval(timer);

}else{

div.style.left=div.offsetLeft+1+'px';

}

},10)

}

function remove(){

clearInterval(timer);

temer=setInterval(function(){

if(div.offsetLeft==-300){

clearInterval(timer);

}else{

div.style.left=div.offsetLeft-1+'px';

}

},10)

}


}

</script>

</head>

<body>

<div id="div"><span>分享</span></div>

</body>

</html>


写回答 关注

1回答

  • 肖潇筱萧3402552
    2016-05-25 16:27:05
    已采纳

    temer=setInterval(function(){ 定时器这儿写错了?应该是timer?

    共 1 条回复 >

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113923 学习 · 1443 问题

查看课程

相似问题