JavaScript运动

   封装函数匀速运动 stattMove
function startMove(dom, target) {
    clearInterval(timer);
    var iSpeed = target - dom.offsetLeft > 0 ? 7 : -7,
                         timer;  
    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);
}

猜你喜欢

转载自blog.csdn.net/weixin_44270940/article/details/86143463