×

用户注册

已有账号? 登录
javascript

cloud-cola

2020-10-28 21:16

javascript匀速运动代码 203

var oDiv = document.getElementsByTagName('div')[0];
var oSpan = document.getElementsByTagName('span')[0];
var oBtn = document.getElementsByTagName('button')[0];
var timer = null;

oBtn.onclick = function () {
clearInterval(timer);
var iSpeed = oSpan.offsetLeft - oDiv.offsetLeft > 0 ? 7 : -7;
timer = setInterval(function () {
if (Math.abs(oSpan.offsetLeft - oDiv.offsetLeft) < Math.abs(iSpeed)) {
clearInterval(timer);
oDiv.offsetLeft = oSpan.offsetLeft;
} else {
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
}
}, 30);
}


暂无任何评论