js运动 缓冲运动

<!DOCTYPE HTML>
<HTML>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        #div1{
            height:100px;
            width: 100px;
            background: red;
            position:absolute;
            margin-left: 5px;
        }
    </style>
    <script>
    window.onload = function ()
    {
        var odiv = document.getElementById(‘div1‘);
        var oinput = document.getElementsByTagName(‘input‘)[0];

        oinput.onclick = function ()
        {

            ToMove(odiv,500);
        }

        function ToMove(obj,goal)
        {
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){
                var icur = obj.offsetLeft;
                if(icur >= goal)
                {
                    clearInterval(obj.timer);
                }
                else
                {
                    var speed = (goal - obj.offsetLeft) / 8;
                    speed = speed > 0 ? Math.ceil(speed): Math.floor(speed);
                    obj.style.left = obj.offsetLeft + speed + ‘px‘;
                }
            },50);
        }
    }
    </script>
    <body>
        <input type="button" value = "提交">
        <div id="div1"></div>
    </body>
</HTML>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。