簡単な運動効果を自分でカプセル化

1733 ワード

/**
 *         
 * @param obj         
 * @param json              
 * @param fn        ,             ,       
 */
function motion(obj, json,fn) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
        //         true ,          ,       ,       
        var onOff = true;
        for ( var attr in json ){
            var current = null;
            //      
            if (attr = "opacity") {
                current = parseInt(getStyle(obj, attr)) * 100;
            } else {
                current = parseInt(getStyle(obj, attr));
            };
            //     
            var speed = ( json[attr] - current ) / 7;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            //                 for  
            if (json[attr] != current) {
                flag = false;
            };
            if (attr = "opacity") {
                obj.style.opacity = ( current + speed ) / 100;
                obj.style.filter = "alpha(opacity = " + ( current + speed ) + ")";
            } else if (attr == "zIndex"){
                obj.style.zIndex = json[attr];
            }else {
                obj.style[attr] = current + speed + "px";
            };
        };
        //            
        if( onOff ){
            clearInterval(obj.timer);
            if (fn) {
                fn();
            };
        };
    }, 50);};
/**
 *      CSS  
 * @param obj         
 * @param attr        
 * @returns {Number}
 */
function getStyle(obj, attr) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(obj.false)[attr];
    } else {
        return obj.currentStyle[attr];
    };
};