问答详情
源自:2-1 JS速度动画

关于取消定时器——clearInterval

<style type="text/css">
    *{margin: 0;padding: 0;}
    #box1{width: 200px;height:200px;position: relative;left: -200px;top: 0;background-color: red;}
    #box1 span{width: 20px;height: 50px;background-color: blue;position: absolute;left: 200px;top: 75px;}
</style>
<script>
    window.onload=function(){
        var oDiv=document.getElementById("box1");
        oDiv.onmouseover=function(){
            starMove();
        };
    };
    var timer=null;
    function starMove(){
        clearInterval(timer);
        var oDiv=document.getElementById("box1");
        timer=setInterval(function(){
            if(oDiv.offsetLeft==0){
                clearInterval(timer);
            }
            else {
                oDiv.style.left=oDiv.offsetLeft+1+"px";
            }
        },30)
    }

没有设置*{margin: 0;padding: 0;}时为什么clearInterval(timer)没有作用

提问者:myeclipse0 2016-05-21 19:17

个回答

  • myeclipse0
    2016-05-21 22:55:19

    我写的是对的,我的意思是去掉*{margin:0;padding:0}为什么会导致定时器取消不起作用

  • Barry0
    2016-05-21 21:31:00

    我用你的代码测试了下是可以的