阅读背景:

动画函数,为任意一个元素移动到指定的目标位置

来源:互联网 

一、动画缓冲函数

/**
 * 动画函数
 * 任意一个元素移动到指定的目标位置
 * @param {*} element 任意一个元素
 * @param {*} target  目标位置(number类型)
 */
function animate(element, target) {
    // 先清理定时器
    clearInterval(element.timeId);
    element.timeId = setInterval(function () {
        // 获取移动元素当前位置
        var current = my$("dv").offsetLeft;
        // 每次移动距离
        var step = 9;
        step = target > current ? step : -step;
        // 移动后的距离
        current +=step;
        // 判断是否到达目标位置
        if(Math.abs(target - current) > Math.abs(step)){
            my$("dv").style.left = current + "px";
        }else{
            clearInterval(element.timeId);
            my$("dv").style.left = target + "px";
        }
    }, 20);
}
/**
 * 动画函数
 * 任意一个元素移动到指定的目标位置



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: