缓动.js(含回调)

function animation(object, target, callback) {
    clearInterval(object.timer)
    // 避免累加定时器的情况出现
    object.style.left = 0
    // 每次点击返回起点
    object.timer = setInterval(function () {
        step = (target - object.offsetLeft) / 10
        step = step > 0 ? Math.ceil(step) : Math.floor(step)
        if (object.offsetLeft >= target) {
            clearInterval(object.timer)
            if (callback) {
                callback()
                // 如果有回调函数,则调用
            }
        }
        object.style.left = object.offsetLeft + step + "px"
    }, 15)
}

猜你喜欢

转载自blog.csdn.net/m0_45293340/article/details/126596259