<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
</style>
</head>
<body>
<div style="width:100px;height:100px;background:red;position:absolute"></div>
<button id="btn" style="margin-top:150px;">121</button>
<span style="position:absolute;left:300px;height:100px;width:1px;background:black;top:0"></span>
<script>
var oDiv = document.getElementsByTagName('div')[0];
var oBtn = document.getElementById('btn');
var timer = null;
oBtn.onclick = function(){
startMove(oDiv,300)
}
function startMove(dom,target){
clearInterval(timer)
var iSpeed = target - dom.offsetLeft > 0 ? 7 : -7;
timer = setInterval(function(){
if(Math.abs(target-dom.offsetLeft) < Math.abs(iSpeed)){
clearInterval(timer)
dom.style.left=target+'px'
}else{
dom.style.left = dom.offsetLeft + iSpeed + 'px'
}
},30)
}
</script>
</body>
</html>