为了账号安全,请及时绑定邮箱和手机立即绑定

clearInterval(timer);不起作用

<!DOCTYPE html>
<html>
    <head>
        <title>Date TEST</title>
        <style>
            #div1{
                left:-200px;
                width: 200px;
                height: 100px;
                position: relative;
                background-color: red;
            }
            #div1 span{
                width: 20px;
                height: 50px;
                background: blue;
                position: absolute;
                left: 200px;
                top: 75px;
            }
        </style>
    </head>
    <body>
        <div id="div1">This is a div for testing animation</br>
            <span>TESTING</span>
        </div>
    </body>
    <script>
        window.onload = function(){
            var oDiv = document.getElementById('div1');
            oDiv.onmouseover = function(){
                startMove();
            }
        }
        var timer = null;
        function startMove(){
            clearInterval(timer);
            var oDiv = document.getElementById('div1');
            timer = setInterval(function(){
                if(oDiv.offsetLeft == 0){
                    clearInterval(timer);
                }
                else
                {
                    oDiv.style.left = oDiv.offsetLeft + 1 + 'px';
                }
            }, 30) 
        }
    </script>
</html>


正在回答

2 回答

body本身的margin属性没有reset

你在CSS里加一句 *{margin:0;padding:0;}

0 回复 有任何疑惑可以回复我~
#1

刘颜 提问者

非常感谢!
2016-10-31 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

clearInterval(timer);不起作用

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信