javaScript动画函数的封装

function animateBian(element, json, fn) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var flag = true;
for (var attr in json) {
if (attr == “opacity”) {
var current = parseInt(getStyle(element, attr) * 100);

            var target = json[attr] * 100;

            var step = (target - current) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step)

            current += step;
            console.log(current);

            element.style[attr] = current / 100;
        } else if (attr == "z-index") {
            var current = parseInt(getStyle(element, attr));

            var target = json[attr];

            var step = (target - current) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step)

            current += step;
            console.log(current);

            element.style[attr] = current;
        } else {
            var current = parseInt(getStyle(element, attr));
            var target = json[attr];
            var step = (target - current) / 10;
            step = step > 0 ? Math.ceil(step) : Math.floor(step)
            current += step;
            element.style[attr] = current + "px";
        }
        if (current != target) {
            flag = false;
        }
    }
    if (flag) {
        clearInterval(element.timeId);
        //所有属性达到目标后才调用
        if (fn) {
            fn();
        }
    }
    //测试代码
    //console.log("目标位置" + target + ",当前位置" + current + ",每次移动的步数" + step);
}, 20)

}

猜你喜欢

转载自blog.csdn.net/weixin_44387879/article/details/86499351