为什么往左只回了一下,然后就开始来回动,求大神帮忙解答

来源:2-1 JS速度动画

y年华游你

2016-03-02 15:57

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>速度动画</title>
</head>
<style>

    body,div,span{
        margin:0;
        padding:0;
    }
    #div1{
        width:200px;
        height:200px;
        background:red;
        position:relative;
        left:-200px;
        top:0;
    }
    #share{
        width:20px;
        height:50px;
        background:blue;
        position:absolute;
        left:200px;
        top:75px;
    }
</style>

<script>
    window.onload=function(){
        var oDiv=document.getElementById('div1');
        oDiv.onmouseover=function(){
            startMove();
        }
        oDiv.onmouseout=function(){
            startMove1();
        }
    }
    var timer=null;
    
    function startMove(){
        clearInterval(timer);//防止每次鼠标经过的时候都要出发定时器
        var oDiv=document.getElementById('div1');
        setInterval(function(){//定时器
            if(oDiv.offsetLeft>=0){
                clearInterval(timer);//清空/暂停定时器
            }else{
                oDiv.style.left=oDiv.offsetLeft+10+'px';
            }
        },30)
    }
    
    function startMove1(){
        clearInterval(timer);//防止每次鼠标经过的时候都要出发定时器
        var oDiv=document.getElementById('div1');
        setInterval(function(){//定时器
            if(oDiv.offsetLeft<=-200){
                clearInterval(timer);//清空/暂停定时器
            }else{
                oDiv.style.left=oDiv.offsetLeft-10+'px';
            }
        },30)
    }
    
</script>


<body>
    <div id="div1">
        <span id="share">分享</span>
    </div>
</body>
</html>

写回答 关注

2回答

  • 奈奈我喜欢你呀
    2016-03-02 16:52:09
    已采纳

    你的代码应该修改为

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>速度动画</title>
    </head>
    <style>
    
        body,div,span{
            margin:0;
            padding:0;
        }
        #div1{
            width:200px;
            height:200px;
            background:red;
            position:relative;
            left:-200px;
            top:0;
        }
        #share{
            width:20px;
            height:50px;
            background:blue;
            position:absolute;
            left:200px;
            top:75px;
        }
    </style>
    
    <script>
        window.onload=function(){
            var oDiv=document.getElementById('div1');
            oDiv.onmouseover=function(){
                startMove();
            }
            oDiv.onmouseout=function(){
                startMove1();
            }
        }
        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+10+'px';
                }
            },30)
        }
        
        function startMove1(){
            clearInterval(timer);//防止每次鼠标经过的时候都要出发定时器
            var oDiv=document.getElementById('div1');
            timer=setInterval(function(){//定时器
                if(oDiv.offsetLeft==-200){
                    clearInterval(timer);//清空/暂停定时器
                }else{
                    oDiv.style.left=oDiv.offsetLeft-10+'px';
                }
            },30)
        }
        
    </script>
    
    
    <body>
        <div id="div1">
            <span id="share">分享</span>
        </div>
    </body>
    </html>


    y年华游你

    非常感谢!

    2016-03-03 10:26:41

    共 1 条回复 >

  • 韩世林
    2016-03-02 16:45:40

    你这个错误很低级,你要清除定时器,可你的timer是启动的定时器吗?你需要将启动定时给timer才能清除。timer=setInterval();然后clearInterval(timer);

JS动画效果

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

113931 学习 · 1443 问题

查看课程

相似问题