js實現減速運動

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   div   art   sp   

這個實現效果是在老師的提點下完成的,當時自己一直陷入在怎樣才能夠實現勻減速運動中不能出來,代碼中的亮點已經表明……歡迎大家指正交流……

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>減速運動</title>    <style>        *{            margin:0;            padding:0;        }        #oplay{            width:200px;            height:200px;            position: absolute;            left:0;            background-color: #B7B7B7;        }        #start_btn{            position: absolute;            top:220px;            font-size: 16px;        }    </style></head><body>    <div id="oplay"></div>    <button id="start_btn">開始運動</button>    <script>        var oplay = document.getElementById("oplay");        var start_btn = document.getElementById("start_btn");        var interval,speed;        start_btn.onclick = function(){            clearInterval(interval);            interval = setInterval(function(){                if(oplay.offsetLeft>=300){                    clearInterval(interval);                }else{                    //這個地方是關鍵,怎樣才能夠使speed在移動元素在停止的時候正好速度為0                    speed = (300-oplay.offsetLeft)/10;                    oplay.style.left = oplay.offsetLeft+speed+"px";                }            },30);        }    </script></body></html>

 

js實現減速運動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.