您的位置:首页 > Web前端 > JavaScript

JavaScript之元素在屏幕从左到右移动的的示例

2015-10-26 19:20 549 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息