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

我设置speed为-1或者-5的时候时移出,而不是移入

<!DOCTYPE html>
<html>
    <head>
        <title>Date TEST</title>
        <style>
            #div1{
                left: 300px;
                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(-10, 100);
            }
        }
        var timer = null;
        function startMove(speed, Target){
            clearInterval(timer);
            var oDiv = document.getElementById('div1');
            timer = setInterval(function(){
                if(oDiv.offsetLeft === Target){
                    clearInterval(timer);
                }
                else
                {
                    oDiv.style.left = oDiv.offsetLeft + speed + 'px';
                }
            }, 30) 
        }
    </script>
</html>


提问者:刘颜 2016-10-30 19:48

个回答

  • 慕少1442876
    2016-10-30 20:12:20
    已采纳

    负值是向左运动,向左不就是移出么