琴& 2019-01-16 14:25 采纳率: 33.3%
浏览 349
已采纳

为什么这个导航栏左右摇摆?

<!DOCTYPE html>
<html>
    <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <title>分享到</title>
        <style>
            #div1
            {
                width: 100px;
                height: 200px;
                background-color: red;
                position: absolute;
                left: -100px;
            }
            #div1 span
            {
                width: 30px;
                height: 70px;
                background-color: yellow;
                position: absolute;
                left: 100px;
                top:50px;
                text-align: center;
            }
        </style>
        <script type="text/javascript">
        window.onload=function()
        {
            var oDiv = document.getElementById('div1');

            oDiv.onmouseover=function ()
            {
                startMove(0);
            }
            oDiv.onmouseout=function ()
            {
                startMove(-100);
            }
        }
        var timer=null;

        function startMove(iTarget)
        { 
            var oDiv = document.getElementById('div1');

            clearInterval(timer);
            timer=setInterval(function () {
                var iSpeed=0;

                if(oDiv.offsetLeft < iTarget)
                {
                    iSpeed = 10;
                }
                else
                {
                    iSpeed = -10;
                }
                if(oDiv.offetLeft==iTarget)
                {
                    clearInterval(timer);
                }
                else
                {
                    oDiv.style.left=oDiv.offsetLeft + iSpeed + 'px';
                }
            },200);
        }
        </script>
    </head>
    <body>
        <div id="div1">
            <span></span>
        </div>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • `奋力前行 2019-01-16 14:51
    关注

    if(oDiv.offsetLeft < iTarget)
    {
    iSpeed = 10;
    }
    else if(oDiv.offsetLeft > iTarget)
    {
    iSpeed = -10;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?