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,50);
        }

        function ToMove(obj,goal,speed)
        {
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){
                var icur = obj.offsetLeft;
                if(icur >= goal)
                {
                    clearInterval(obj.timer);
                }
                else
                {
                    speed *= 0.9;
                    obj.style.left = obj.offsetLeft + speed + 'px';
                }
            },50);
        }
    }
    </script>
    <body>
        <input type="button" value = "提交">
        <div id="div1"></div>
    </body>
</HTML>

 

posted @ 2015-03-14 19:04  mayufo  阅读(206)  评论(0编辑  收藏  举报