標籤: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實現減速運動