<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> #div1 {width:30px; height:20px; background:red; position:absolute; top:150px; left:50px;} #div2 {width:2px; height: 500px; background-color: blue;position: absolute;left: 100px;} #div3 {width:2px; height: 500px; background-color: blue;position: absolute;left: 300px;} </style> <script> var timer=null; function startDown() { var oDiv=document.getElementById('div1'); var speed = 1; clearInterval(timer); timer=setInterval(function (){ if(oDiv.offsetTop>=300) { clearInterval(timer); } else { oDiv.style.top=oDiv.offsetTop+speed+'px'; } }, 30); } function startUp() { var oDiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function (){ var speed=-1; if(oDiv.offsetTop<=50) { clearInterval(timer); } else { oDiv.style.top=oDiv.offsetTop+speed+'px'; } }, 30); } function startLeft() { var oDiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function (){ var speed=5; if(oDiv.offsetLeft>=300) { clearInterval(timer); } else { oDiv.style.left=oDiv.offsetLeft+speed+'px'; } }, 30); } function startRight() { var oDiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function (){ var speed=-5; if(oDiv.offsetLeft<=100) { clearInterval(timer); } else { oDiv.style.left=oDiv.offsetLeft+speed+'px'; } }, 30); } </script> </head> <body> <input id="btn1" type="button" value="开始往下" onclick="startDown()" /> <input id="btn2" type="button" value="开始往上" onclick="startUp()" /> <input id="btn3" type="button" value="开始往右" onclick="startLeft()" /> <input id="btn4" type="button" value="开始往左" onclick="startRight()" /> <div id="div1"> </div> <div id="div2"> </div> <div id="div3"> </div> </body> </html>
1116学习记录 简单练车模型
最新推荐文章于 2025-11-06 08:58:31 发布
本文介绍了一个使用HTML和JavaScript实现的简单动画案例,通过控制div元素的移动方向来演示上下左右移动的效果。文章提供了完整的HTML页面代码,包括内联样式定义及JavaScript函数用于控制动画过程。
3408

被折叠的 条评论
为什么被折叠?



