The example in this article describes the simple implementation method of JavaScript elastic motion effect. Share it with everyone for your reference, the details are as follows:
Principle of elastic motion: acceleration motion, deceleration motion, friction motion
The screenshot of the running effect is as follows:
The example code is as follows:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> #div1{ width:100px; height:100px; background:red; position:absolute; left:0; top:50px;} </style> <script> window.onload = function() { var oBtn = document.getElementById('btn1'); var oDiv = document.getElementById('div1'); oBtn.onclick = function() { startMove(oDiv, 300); }; }; var iSpeed = 0; var left = 0; function startMove(obj, iTarget) { clearInterval(obj.timer); obj.timer = setInterval(function(){ iSpeed += (iTarget - obj.offsetLeft)/5; iSpeed *= 0.7; left += iSpeed; if(Math.abs(iSpeed)<1 && Math.abs(left-iTarget)<1){ clearInterval(obj.timer); obj.style.left = iTarget + 'px'; }else{ obj.style.left = obj.offsetLeft + iSpeed + 'px'; } }, 30); } </script> </head> <body> <input id="btn1" type="button" value="运动" /> <div id="div1"></div> <div style="width:1px; height:300px; background:black; position:absolute; top:0; left:300px; "></div> </body> </html>
For more content related to JavaScript motion effects, please view the special topic on this site: " Summary of JavaScript motion effects and techniques"
I hope this article will be helpful to everyone in JavaScript programming.