加上remove后失效,注释掉remove又可以了,为什么,大神们帮帮忙

来源:2-1 JS速度动画

qq_夕阳_11

2016-05-29 00:31

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>

<div id="div">


</div>

<script type="text/javascript">

window.onload=function(){

var oDiv=document.getElementById("div");

oDiv.onmouseover=function(){


startMove();

}

oDiv.onmouseout=function(){


reMove();

}


}

var timer=null;

function startMove(){

clearInterval(timer);

           var oDiv=document.getElementById("div");

           

           timer=setInterval(function(){

            if (oDiv.offsetLeft==500) {

            clearInterval(timer);

            } 

            else {

            oDiv.style.left=oDiv.offsetLeft+1+"px";

            }

 

            },10)

        function reMove(){

clearInterval(timer);

           var oDiv=document.getElementById("div");

           

           timer=setInterval(function(){

            if (oDiv.offsetLeft==0) {

            clearInterval(timer);

            } 

            else {

            oDiv.style.left=oDiv.offsetLeft-1+"px";

            }

 

            },10)

            

}

</script>

</body>

</html>


写回答 关注

1回答

  • 苍白的尾巴
    2016-05-29 20:14:27

    在浏览器里用F12调试之后发现你在 startMove()这个函数处少写了“ } ”。38行后面。

JS动画效果

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

113917 学习 · 1502 问题

查看课程

相似问题