欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js完美运动框架

程序员文章站 2022-03-27 13:06:26
...

// 完美运动框架:

function startMove(obj,json,complete){
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
        var isYes = true;
        for(var attr in json){
            var iCur = null;
            if(attr == 'opacity'){
                iCur = parseInt(getStyle(obj,'opacity') * 100);
            }else{
                iCur = parseInt(getStyle(obj,attr));
            }
            var speed = (json[attr] - iCur) / 8;
            speed = speed > 0? Math.ceil(speed): Math.floor(speed);
            if(attr == 'opacity'){
                iCur += speed;
                obj.style.opacity = iCur / 100;
                obj.style.filter = 'alpha(opacity=' + iCur + ')';
            }else{
                obj.style[attr] = iCur + speed + 'px';
            }
            if(iCur != json[attr]){
                isYes = false;
            }
        }
        if(isYes == true){
            clearInterval(obj.timer);
            if(complete){
                complete.call(obj);
            }
        }
    },30);
    function getStyle(element,csstype){
        return  element.currentStyle ? element.currentStyle[cssType] : getComputedStyle(element)[cssType];
    }
    ["获取样式的简单实现方法"](https://www.jb51.net/article/92551.htm)
}