效果图
![在这里插入图片描述](https://img-blog.csdnimg.cn/e9ee5887af684d0da8eef4de12686514.gif)
结构
<div></div>
<span></span>
CSS
div {
position: absolute;
left: 0;
width: 100px;
height: 100px;
background-color: pink;
}
span {
position: absolute;
left: 0;
top: 100px;
background-color: red;
width: 100px;
height: 100px;
}
js
function animate (obj, target)
{
var timer = setInterval(function ()
{
if (obj.offsetLeft >= target)
{
clearInterval(timer);
}
obj.style.left = obj.offsetLeft + 1 + 'px';
}, 30)
}
var div = document.querySelector('div');
var span = document.querySelector('span');
animate(div, 300);
animate(span, 300);