样式
.box {
width: 100px;
height: 100px;
background-color: red;
position: fixed;
left: 0;
top: 0;
}
js
<body>
<div class="box"></div>
<script>
let box = document.querySelector('.box');
function interval(target) {
let left = box.offsetLeft;
let t = setTimeout(function() {
left += 13;
box.style.left = left + 'px';
if (left >= target) {
box.style.left = target + 'px';
return;
}
interval(target);
}, 1000 / 60)
}
document.onclick = function() {
interval(700)
}
</script>
</body>