代码:
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>无标题文档</title> <style> #div1 { width: 100px; height: 100px; background: #ff4b46; position: absolute; left: 100px; top: 50px; } </style> <script> function startMove(){ setInterval(function(){ var oDiv=document.getElementById("div1"); var speed=(1000-oDiv.offsetLeft)/10; speed=speed>0?Math.ceil(speed):Math.floor(speed); oDiv.style.left=oDiv.offsetLeft+speed+"px"; document.title=oDiv.offsetLeft+‘,‘+speed; },30); } </script></head><body><input type="button" value="开始运动" onclick="startMove()"><div id="div1"> </div> </body></html>
运行结果:
初始界面:
点击鼠标之后界面:
时间: 2024-10-12 23:22:57