<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#box{
width: 100px;
height: 100px;
background-color: pink;
position: absolute;
top: 100px;
left: 0;
}
</style>
</head>
<body>
<button id="btn">开始</button>
<div id="box"></div>
</body>
</html>
<script>
var btn = document.getElementById("btn");
var box = document.getElementById("box");
var num = 0;
var timer = null;
btn.onclick = function () {
timer = setInterval(function(){
num++;
num>=500 ? clearInterval(timer) : box.style.left = num + "px";
},10);
}
</script>