欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)勻速運(yùn)動(dòng)的代碼實(shí)例

 更新時(shí)間:2013年11月29日 16:45:42   作者:  
這篇文章主要介紹了JS實(shí)現(xiàn)勻速運(yùn)動(dòng)的代碼實(shí)例,有需要的朋友可以參考一下

效果:

 

思路:

利用setInerval()計(jì)時(shí)器,進(jìn)行運(yùn)動(dòng)。然后關(guān)鍵的一點(diǎn)是在最后停止的時(shí)候給它一個(gè)填充縫隙的判斷。

代碼:

復(fù)制代碼 代碼如下:

<head runat="server">
    <title></title>
    <style type="text/css">
        #div1
        {
            width: 100px;
            height: 100px;
            background: #0000FF;
            position: absolute;
            left: 800px;
            top: 100px;
        }
        #div200
        {
            width: 1px;
            height: 400px;
            background: #FF0000;
            position: absolute;
            left: 200px;
        }
        #div500
        {
            width: 1px;
            height: 400px;
            background: #FF0000;
            position: absolute;
            left: 500px;
        }
    </style>
    <script type="text/javascript">
        function move(end) {
            var oDiv = document.getElementById('div1');
            var timer = null;
            timer = setInterval(function () {
                var speed = (end - oDiv.offsetLeft) / 5;        //根據(jù)終點(diǎn)和offsetLeft取出運(yùn)動(dòng)的速度
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);       //進(jìn)位取整,小數(shù)位變?yōu)檎唬?BR>                //                if (oDiv.offsetLeft <= end) {
                //                    clearInterval(timer);
                //                }
                //                else {
                //                    oDiv.style.left = oDiv.offsetLeft + speed + 'px';
                //                }
                if (Math.abs(end - oDiv.offsetLeft) <= speed) { //由于在停止的時(shí)候最后會(huì)出現(xiàn)小的縫隙,或者說(shuō)沒(méi)有完全的到達(dá)指定地點(diǎn),所以要小于它的速度
                    clearInterval(timer);                       //當(dāng)距離小于速度時(shí),讓計(jì)時(shí)器停止
                    oDiv.style.left = end + 'px';           //在停止后填充縫隙。
                }
                else {
                    oDiv.style.left = oDiv.offsetLeft + speed + 'px';       //移動(dòng)DIV
                }
            }, 30)
        }
    </script>
</head>
<body>
    <input type="button" id="btn1" value="到500的位置" onclick="move(500);" />
    <input type="button" id="btn2" value="到200的位置" onclick="move(200);" />
    <div id="div1">
    </div>
    <div id="div200">200
    </div>
    <div id="div500">500
    </div>
</body>

相關(guān)文章

最新評(píng)論