![javascript平时小例子①(移动的小div) javascript平时小例子①(移动的小div)](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
css样式:
#box{
width: 300px;
height: 300px;
background: deepskyblue;
position: absolute;
margin-right: 20px;
}
html布局:
<div id="box"></div>
js部分:
window.onload=function(){
abc();
setInterval(abc,50)
function abc(){
var oDiv=document.getElementById("box");
oDiv.style.left=oDiv.offsetLeft+10+"px";
}
}