<!doctype html> <html> <head> <meta charset="utf-8"> <title>匀速运动</title> <style> #div1{width: 200px; height: 200px; background: blue; position: absolute; top:50px; left: 0px;} </style> <script type="text/javascript"> var timer = null; function StartMove() { clearInterval(timer); var oDiv = document.getElementById(‘div1‘); timer = setInterval(function(){ var speed = 10; if(oDiv.offsetLeft>=300) { clearInterval(timer); } else { oDiv.style.left = oDiv.offsetLeft + speed + ‘px‘; } },30); } </script> </head> <body> <input type = "button" id = "btn1" value = "开始运动" onclick = "StartMove()"/> <div id = "div1"></div> </body> </html>
原文:http://www.cnblogs.com/pjw2017/p/6252126.html