<div style="background-color: green;width:200px;height:100px;position:absolute;left:0;top:0"></div>
<script>
//让盒子移动
//获取当前盒子的left和top值(getComputedStyele(elem,null).left),然后再这基础之上加上一定的偏移量,然后让其循环往上加setInterval(function(){},100);
var div=document.getElementsByTagName('div')[0];
function getStyle(elem,prop){
if(window.getComputedStyle){
return(window.getComputedStyle(elem,null)[prop]);
}else{
return elem.currentStyle[prop];
}
}
//让盒子动起来setInterval()
var speed=7;
var timer= setInterval(function(){
speed+=speed/6;
console.log(speed);
div.style.left=parseInt(getStyle(div,"left"))+speed+"px";
if(parseInt(div.style.left)>1000){
clearInterval(timer);
}
},100);
盒子移动
最新推荐文章于 2022-09-14 10:46:46 发布