<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
.box{
position:absolute;
top:100px;
left:0;
width:100px;
height:100px;
background-color:skyblue;
}
input{
height: 50px;
}
</style>
</head>
<body>
<br>
<input type="button" value="开始将下面的元素向右移动" id="start">
<input type="button" value="点击停止元素的移动" id="ttt">
<div class="box" id="box">
</div>
<script>
var start = document.getElementById("start");
var box = document.getElementById("box");
var ttt = document.getElementById("ttt");
var nowleft = 0;
var temer;
start.onclick = function(){
clearInterval(temer);
temer = setInterval(function(){
nowleft += 35;
if(nowleft >= 500){
nowleft = 500;
clearInterval(temer);
}
box.style.left = nowleft + "px";
},100);
};
ttt.onclick = function(){
clearInterval(temer);
};
</script>
</body>
</html>