<html> <head> </head> <body> <style> #ball{background:red;border-radius:50%;width:100px;height:100px;margin:10px;position:absolute} </style> <div id="ball"></div> <button onclick="move()">移动</button> <button onclick="stop()">停止</button> <script> var aa = setInterval(function(){ move() },500) // var aa = setInterval(move,500) function move(){ var ball= document.getElementById('ball') ball.style.left = ball.offsetLeft + 100 +"px" } function stop(){ clearInterval(aa); } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例