<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <head> <title>javascript实现的简单动画</title> <style type="text/css"> #mydiv { width:50px; height:50px; background-color:green; position:absolute; } </style> <script type="text/javascript"> window.onload=function() { var mydiv=document.getElementById("mydiv"); var start=document.getElementById("start"); var stopmove=document.getElementById("stopmove"); var x=0; var flag; function move() { x=x+1; mydiv.style.left=x+"px"; } start.onclick=function() { clearInterval(flag); flag=setInterval(move,20); } stopmove.onclick=function() { clearInterval(flag); } } </script> <body> <input type="button" id="start" value="开始运动" /> <input type="button" id="stopmove" value="停止运动" /> <div id="mydiv"></div> </body> </html>
在线运行
代码解释:
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号