首页 > Web开发 > 详细

js运动 缓冲运动

时间:2015-03-14 19:59:21      阅读:312      评论:0      收藏:0      [点我收藏+]
<!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>

 

js运动 缓冲运动

原文:http://www.cnblogs.com/mayufo/p/4338081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!