console
window.onload = function() {
var btn = document.getElementById('btn');
var png = document.getElementById('png');
var sudu = 10;
btn.onclick = function() {
setInterval(function() {
var laoleft = png.style.left;
var xinleft = parseInt(laoleft) + sudu;
png.style.left = xinleft + 'px';
if (xinleft == 600 || xinleft == 50) {
sudu = sudu * -1;
}
},
20)
}
}
<input id="btn" type="button" value="点击来回运动">
<div id="png" style="left: 50px;">
</div>
#png {
width: 200px;
height: 200px;
background-color: blue;
position: absolute;
top: 80px;
}
#btn {
width: 100px;
height: 50px;
background-color: red;
color: #fff;
border: none;
font-size: 14px;
}