js让一个div均匀移动5像素的方法?
functiontest(){varobj=document.getElementById("a");varn=obj.style.left;varre=^d+/;var...
function test(){
var obj=document.getElementById("a");
var n=obj.style.left;
var re=^d+/;
var a=re.exec(n);
n=eval(a[0]);
n+=5;
obj.style.left=n+"px"
.obj.style.fontsize=26+"px";
}
window.onload=function(){
setTimeout("test()",50);
} 展开
var obj=document.getElementById("a");
var n=obj.style.left;
var re=^d+/;
var a=re.exec(n);
n=eval(a[0]);
n+=5;
obj.style.left=n+"px"
.obj.style.fontsize=26+"px";
}
window.onload=function(){
setTimeout("test()",50);
} 展开
1个回答
展开全部
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="a" style="width:20px;height:20px;background-color: red;position: relative;display: block;"></div>
<script>
var x;
var y;
var obj;
var timer;
function test(){
obj=document.getElementById("a");
x=obj.clientLeft;
y=obj.clientTop;
obj.style.fontsize=26+"px";
}
window.onload=function(){
test();
timer=setInterval(moveDiv,0);
}
function moveDiv(){
x+=5;
// y+=2;
// obj.style.top=y+"px";
obj.style.left=x+"px";
console.log(x);
if(x>1040){
clearInterval(timer);
console.log(obj);
}
}
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询