<html>
<head>
<title>Animating a DOM Element</title>
<style>
#div1{
position:absolute;
border:1px solid black;
width:50px;
height:50px;
left:0px;
top:100px;
}
</style>
<script>
var timer1 = null;
var el = null;
function moveItRight(){
if(parseInt(el.style.left) > (screen.width-50))
el.style.left = 0;
el.style.left = parseInt(el.style.left)+2+'px';
timer1 = setTimeout(moveItRight,25); // 在指定的毫秒数后调用函数
}
window.onload = function(){
el = document.getElementById("div1");
el.style.left = '50px';
moveItRight();
}
</script>
</head>
<body>
<div id="div1"> Move It! </div>
</body>
</html>
JavaScript之元素在屏幕从左到右移动的的示例
最新推荐文章于 2023-12-22 21:06:48 发布