zoukankan      html  css  js  c++  java
  • jqueyr fx源码(修改)

    1. /*  
    2.  * author:prk date:2008-08-07 comment:analyse the fx of jQuery.  
    3.  *   
    4.  */  
    5. jQuery.fn.extend({   
    6.        
    7.     // show(speed,[callback])   
    8.     // 以优雅的动画隐藏所有匹配的元素,并在显示完成后可选地触发一个回调函数。   
    9.     // 可以根据指定的速度动态地改变每个匹配元素的高度、宽度和不透明度。   
    10.     // 显示隐藏的匹配元素 show()   
    11.     show: function(speed,callback){   
    12.         return speed ?   
    13.             this.animate({   
    14.                 height: "show",  "show", opacity: "show"  
    15.             }, speed, callback) :   
    16.   
    17.             this.filter(":hidden").each(function(){   
    18.                 this.style.display = this.oldblock || "";   
    19.                 if ( jQuery.css(this,"display") == "none" ) {   
    20.                     var elem = jQuery("<" + this.tagName + " />").appendTo("body");   
    21.                     this.style.display = elem.css("display");// 默认的显示的display   
    22.                     // handle an edge condition where css is - div {   
    23.                     // display:none; } or similar   
    24.                     if (this.style.display == "none")// 处理显式地设定了该tag不显示,只好采用b   
    25.                         this.style.display = "block";   
    26.                     elem.remove();// 上面这些的处理有没有必要呢?   
    27.                 }   
    28.             }).end();// 回到前一个jQuery对象   
    29.     },   
    30.        
    31.     // 与show相反   
    32.     hide: function(speed,callback){   
    33.         return speed ?   
    34.             this.animate({   
    35.                 height: "hide",  "hide", opacity: "hide"  
    36.             }, speed, callback) :   
    37.   
    38.             this.filter(":visible").each(function(){   
    39.                 this.oldblock = this.oldblock || jQuery.css(this,"display");   
    40.                 this.style.display = "none";   
    41.             }).end();   
    42.     },   
    43.   
    44.     // Save the old toggle function   
    45.     _toggle: jQuery.fn.toggle,   
    46.       /*  prk/彭仁夔            转载请注明出处 <SPAN><A href="http://jljlpch.javaeye.com/*/">http://jljlpch.javaeye.com/*/</A></SPAN>  
    Java代码 复制代码
    1.     // 切换元素的可见状态。   
    2.     // 如果元素是可见的,切换为隐藏的;如果元素是隐藏的,切换为可见的。   
    3.        
    4.     // 每次点击后依次调用函数。   
    5.     // 如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数,   
    6.     // 如果有更多函数,则再次触发,直到最后一个。随后的每次点击都重复对这几个函数的轮番调用。   
    7.     // 可以使用unbind("click")来删除。   
    8.     toggle: function( fn, fn2 ){   
    9.         return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?   
    10.             this._toggle.apply( this, arguments ) :// 原来的toggle   
    11.             (fn ?   
    12.                this.animate({height: "toggle",  "toggle", opacity: "toggle"}, fn, fn2)   
    13.             :  this.each(function(){jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();}   
    14.             // 对每个元素都调用show,或hide函数。   
    15.             )   
    16.         );   
    17.     },   
    18.   
    19.     // 把所有匹配元素的不透明度以渐进方式调整到指定的不透明度,并在动画完成后可选地触发一个回调函数。   
    20.      // 这个动画只调整元素的不透明度,也就是说所有匹配的元素的高度和宽度不会发生变化。   
    21.     fadeTo: function(speed,to,callback){   
    22.         return this.animate({opacity: to}, speed, callback);   
    23.     },   
    24.        
    25.     /**  
    26.      * 用于创建自定义动画的函数。  
    27.      * 这个函数的关键在于指定动画形式及结果样式属性对象。这个对象中每个属性都表示一个可以变化的样式属性(如“height”、“top”或“opacity”)。  
    28.      * 注意:所有指定的属性必须用骆驼形式,比如用marginLeft代替margin-left.  
    29.      * 而每个属性的值表示这个样式属性到多少时动画结束。如果是一个数值,样式属性就会从当前的值渐变到指定的值。如果使用的是“hide”、“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。  
    30.      * 在 jQuery 1.2 中,你可以使用 em 和 % 单位。另外,在 jQuery 1.2 中,你可以通过在属性值前面指定 "+=" 或  
    31.      * "-=" 来让元素做相对运动。  
    32.      *   
    33.      * params (Options) : 一组包含作为动画属性和终值的样式属性和及其值的集合 。 duration (String,Number)  
    34.      * :(可选) 三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000)  
    35.      * easing (String) : (可选) 要使用的擦除效果的名称(需要插件支持).默认jQuery提供"linear" 和 "swing".  
    36.      * callback (Function) : (可选) 在动画完成时执行的函数  
    37.      */  
    38.     animate: function( prop, speed, easing, callback ) {   
    39.         var optall = jQuery.speed(speed, easing, callback);   
    40.   
    41.         return this[ optall.queue === false ? "each" : "queue" ](function(){// 执行each或queue方法   
    42.             var opt = jQuery.extend({}, optall), p,   
    43.                 hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),// 是元素节点且是隐藏的   
    44.                 self = this;// 当前的元素   
    45.        
    46.             for ( p in prop ) {   
    47.                 //如果是完成的状态,就直接调用complate函数   
    48.                 if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )// 已经是   
    49.                     return opt.complete.call(this);//,在用户的callback加上队列的处理   
    50.   
    51.                 if ( ( p == "height" || p == "width" ) && this.style ) {// style中高度,宽度   
    52.                     opt.display = jQuery.css(this"display");// 保存当前元素的display   
    53.                     opt.overflow = this.style.overflow;// 保证没有暗中进行的   
    54.                 }   
    55.             }   
    56.             if ( opt.overflow != null )// 超出部分不见   
    57.                 this.style.overflow = "hidden";   
    58.   
    59.             opt.curAnim = jQuery.extend({}, prop);//clone传入的参数prop   
    60.       
    61.             jQuery.each( prop, function(name, val){// 对当前元素的给定的属性进行变化的操作   
    62.                 var e = new jQuery.fx( self, opt, name );   
    63.                    
    64.                 if ( /toggle|show|hide/.test(val) )// 传参的属性可以用toggle,show,hide,其它   
    65.                 // 调用当前e.show,e.hide,e.val的方法,jQuery.fx.prototype   
    66.                     e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );                     
    67.                 else {// 支持"+=" 或 "-=" 来让元素做相对运动。   
    68.                     var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),   
    69.                         start = e.cur(true) || 0;// 当前元素当前属性的值   
    70.                       // +=" 或 "-=" 来让元素做相对运动。   
    71.                     if ( parts ) {   
    72.                         var end = parseFloat(parts[2]),// 值   
    73.                             unit = parts[3] || "px";// 单位   
    74.                            
    75.                         if ( unit != "px" ) {// 计算开始的值=(end/cur)*start   
    76.                             self.style[ name ] = (end || 1) + unit;   
    77.                             start = ((end || 1) / e.cur(true)) * start;   
    78.                             self.style[ name ] = start + unit;   
    79.                         }   
    80.   
    81.                         if ( parts[1] )// +=/-=,做相对运行   
    82.                             end = ((parts[1] == "-=" ? -1 : 1) * end) + start;   
    83.   
    84.                         e.custom( start, end, unit );// 动画   
    85.                     }    
    86.                     //直接计算start和end的位置来动画。val是数值的end,start当前的属性值。   
    87.                     else  
    88.                         e.custom( start, val, "" );// 动画   
    89.                 }   
    90.             });   
    91.             // For JS strict compliance   
    92.             return true;   
    93.         });   
    94.     },   
    95.   
    96.     // 实现队列操作,为jQuery对象中的每个元素都加type的属性,值为fn.   
    97.     queue: function(type, fn){   
    98.         // 可能看出支持一个参数的fn或array形式的集合其type为默认的fx的形式。   
    99.         if ( jQuery.isFunction(type) || ( type && type.constructor == Array )) {   
    100.             fn = type;   
    101.             type = "fx";   
    102.         }   
    103.         // type不存在,空字符等,无参数,type:string,fn不存在。肯定不是函数,也不是数组   
    104.         if ( !type || (typeof type == "string" && !fn) )   
    105.             return queue( this[0], type );//从queue取出   
    106.   
    107.         return this.each(function(){   
    108.             //fn是数组的形式fn集合,直接构建queue   
    109.             if ( fn.constructor == Array )// 数组的形式   
    110.                 queue(this, type, fn);// 存储在元素的type属性中   
    111.               //取得queue,后push,只有一个立即运行。   
    112.             else {   
    113.                 queue(this, type).push( fn );   
    114.                 if ( queue(this, type).length == 1 )   
    115.                     fn.call(this);   
    116.             }   
    117.         });   
    118.     },   
    119.   
    120.     stop: function(clearQueue, gotoEnd){   
    121.         var timers = jQuery.timers;   
    122.   
    123.         if (clearQueue)   
    124.             this.queue([]);// 清除   
    125.   
    126.         this.each(function(){   
    127.             //倒序是为了能把在loop过程加入timers的当前元素的属性的动画step也给删除。   
    128.             for ( var i = timers.length - 1; i >= 0; i-- )   
    129.                 if ( timers[i].elem == this ) {   
    130.                     if (gotoEnd) timers[i](true);   
    131.                         // 强迫动画结束                          
    132.                     timers.splice(i, 1);   
    133.                 }   
    134.         });   
    135.         // start the next in the queue if the last step wasn't forced   
    136.         if (!gotoEnd)   
    137.             this.dequeue();   
    138.   
    139.         return this;   
    140.     }   
    141.   
    142. });   
    143.   
    144. // Generate shortcuts for custom animations   
    145. jQuery.each({   
    146.     slideDown: { height:"show" },   
    147.     slideUp: { height: "hide" },   
    148.     slideToggle: { height: "toggle" },   
    149.     fadeIn: { opacity: "show" },   
    150.     fadeOut: { opacity: "hide" }   
    151. }, function( name, props ){   
    152.     jQuery.fn[ name ] = function( speed, callback ){   
    153.         return this.animate( props, speed, callback );   
    154.     };   
    155. });   
    156.   
    157. // 为元素加上type的array的属性,或返回取到elem上type的值   
    158. var queue = function( elem, type, array ) {   
    159.     if ( elem ){   
    160.         type = type || "fx";   
    161.         var q = jQuery.data( elem, type + "queue" );   
    162.         if ( !q || array )   
    163.             q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) );   
    164.     }   
    165.     return q;   
    166. };   
    167. // 出列,根据type   
    168. jQuery.fn.dequeue = function(type){   
    169.     type = type || "fx";   
    170.     return this.each(function(){   
    171.         var q = queue(this, type);// 得取type的的值   
    172.         q.shift();// 移出一个   
    173.         if ( q.length )   
    174.             q[0].call( this );   
    175.     });   
    176. };   
    177.   
    178.   
    179. jQuery.extend({   
    180.      // 主要用于辅助性的工作   
    181.     speed: function(speed, easing, fn) {   
    182.         var opt = speed && speed.constructor == Object ? speed : {// 采用紧凑型方式   
    183.             complete: fn || !fn && easing ||   
    184.                 jQuery.isFunction( speed ) && speed,// coplete是至多三个参数的最后一个,当然是Fn.   
    185.             duration: speed,// 持继的时间   
    186.             easing: fn && easing || easing && easing.constructor != Function && easing// 不是Fn   
    187.         };   
    188.   
    189.         opt.duration = (opt.duration && (opt.duration.constructor == Number ?   
    190.             opt.duration :  jQuery.fx.speeds[opt.duration])) // 存在,不是数值,转找   
    191.             || jQuery.fx.speeds._default;// 默认的   
    192.   
    193.         // Queueing   
    194.         opt.old = opt.complete;   
    195.         opt.complete = function(){// 排队的处理   
    196.             if ( opt.queue !== false )//可能通过参数指定queue   
    197.                 jQuery(this).dequeue();//出queue   
    198.             if ( jQuery.isFunction( opt.old ) )   
    199.                 opt.old.call( this );//执行完成之后的回调   
    200.         };   
    201.   
    202.         return opt;   
    203.     },   
    204.   
    205.     // 擦除效果   
    206.     easing: {   
    207.         linear: function( p, n, firstNum, diff ) {   
    208.             return firstNum + diff * p;   
    209.         },   
    210.         swing: function( p, n, firstNum, diff ) {   
    211.             return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;   
    212.         }   
    213.     },   
    214.   
    215.     timers: [],// jQuery.timers   
    216.     timerId: null,   
    217.        
    218.     // 根据参数构成一个对象   
    219.     fx: function( elem, options, prop ){   
    220.         this.options = options;   
    221.         this.elem = elem;   
    222.         this.prop = prop;   
    223.   
    224.         if ( !options.orig )   
    225.             options.orig = {};   
    226.     }   
    227.   
    228. });   
    229.   
    230. jQuery.fx.prototype = {   
    231.   
    232.     // 为元素设值,更新显示   
    233.     update: function(){   
    234.         //可以在显示之前进行自定义的显示操作   
    235.         //这里可以是改变this.now或元素的其它属性。   
    236.         //改变this.now是改变动画的轨迹,改变其它的属性会有更多的效果   
    237.         if ( this.options.step )   
    238.             this.options.step.call( this.elem, this.now, this );   
    239.          //根据this.now来改变/设值当前属性的值。也就改变了样式。   
    240.         (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );   
    241.   
    242.         // 对于高度和宽度,肯定是要能看出效果的,故采用display=block。   
    243.         if ( ( this.prop == "height" || this.prop == "width" ) && this.elem.style )   
    244.             this.elem.style.display = "block";   
    245.     },   
    246.   
    247.     // 当前元素当前属性的值   
    248.     cur: function(force){   
    249.         if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) )   
    250.             return this.elem[ this.prop ];   
    251.   
    252.         var r = parseFloat(jQuery.css(this.elem, this.prop, force));   
    253.         return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;   
    254.     },   
    255.   
    256.     // 开动一个动画   
    257.     custom: function(from, to, unit){   
    258.         this.startTime = now();//动画开始的时候   
    259.         this.start = from;//位置开始点   
    260.         this.end = to;//位置结果点   
    261.         this.unit = unit || this.unit || "px";   
    262.         this.now = this.start;//位置当前点   
    263.         //state是时间间隔在总的duration的比率   
    264.         //pos是按一定算法把时间上的比率折算到位置上的比率   
    265.         this.pos = this.state = 0;   
    266.         //根据this.now位置当前点的值来设定元素的属性显示出来   
    267.         this.update();   
    268.   
    269.         var self = this;   
    270.         function t(gotoEnd){   
    271.             return self.step(gotoEnd);// 调用step(gotoEnd)//本对象的   
    272.         }   
    273.         t.elem = this.elem;//删除的时候做判断用   
    274.         //timers栈是公共的,不同的元素的不同的属性step都是放在这里面。   
    275.         jQuery.timers.push(t);   
    276.             
    277.         if ( jQuery.timerId == null ) {   
    278.             jQuery.timerId = setInterval(function(){   
    279.                 var timers = jQuery.timers;   
    280.                 //倒是觉得这里会有同步冲突的问题。Ext.observable中就有解决方法   
    281.                 for ( var i = 0; i < timers.length; i++ )// 执行timers中所有   
    282.                      
    283.                 //当一个属性的动画完成,或强迫完成的时候,把step从数组中删除.   
    284.                 //同时把i的位置不改变。继续下一个。   
    285.                 if ( !timers[i]() )                    
    286.                         timers.splice(i--, 1);                         
    287.                      
    288.                   //说明还有属性的动画没有完成,step还在timers中。   
    289.                   //那么就不clearInterval,13ms之后再继续。直到数组   
    290.                   //中所有的step都被删除。   
    291.                 if ( !timers.length ) {   
    292.                     clearInterval( jQuery.timerId );   
    293.                     jQuery.timerId = null;   
    294.                 }   
    295.             }, 13);   
    296.         }   
    297.     },   
    298.   
    299.     // Simple 'show' function   
    300.     show: function(){   
    301.         // 保存当前的,以被修改之后能得到初始的值   
    302.         this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );   
    303.         this.options.show = true;//标明是进行show操作   
    304.            
    305.         this.custom(0this.cur());   
    306.            
    307.         //让最开始时以1px的宽或高度来显示。防止内容flash   
    308.         if ( this.prop == "width" || this.prop == "height" )   
    309.             this.elem.style[this.prop] = "1px";        
    310.         jQuery(this.elem).show();   
    311.     },   
    312.   
    313.     // 隐藏   
    314.     hide: function(){   
    315.         // 保存当前的,以被修改之后能得到初始的值   
    316.         this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );   
    317.         this.options.hide = true;//标识是进行hide操作   
    318.            
    319.         this.custom(this.cur(), 0);   
    320.     },   
    321.   
    322.     // 动画的每一个步骤   
    323.     step: function(gotoEnd){   
    324.         var t = now();//运行到当前的时间,因为是13ms才运行一次。   
    325.          // 强行指定结束或当前时间大于startTime+duration   
    326.         if ( gotoEnd || t > this.options.duration + this.startTime ) {   
    327.             this.now = this.end;//当前的位置为结束位置   
    328.             this.pos = this.state = 1;//当前的state,pos的比率为1.最大。   
    329.             this.update();//显示   
    330.             //标识这个属性的动画已经完成   
    331.             this.options.curAnim[ this.prop ] = true;   
    332.             //再一次判断是否完成   
    333.             var done = true;   
    334.             for ( var i in this.options.curAnim )   
    335.                 if ( this.options.curAnim[i] !== true )   
    336.                     done = false;   
    337.   
    338.             if ( done ) {                  
    339.                 if ( this.options.display != null ) {//  恢复overflow   
    340.                     this.elem.style.overflow = this.options.overflow;   
    341.                     // 恢复 display   
    342.                     this.elem.style.display = this.options.display;   
    343.                     //判断其是否恢复成功,   
    344.                     if ( jQuery.css(this.elem, "display") == "none" )   
    345.                         this.elem.style.display = "block";   
    346.                 }   
    347.   
    348.                 //如果是hide的操作   
    349.                 if ( this.options.hide )   
    350.                     this.elem.style.display = "none";   
    351.                    
    352.                     //如果元素已经show或hide,恢复其动画改变的属性   
    353.                 if ( this.options.hide || this.options.show )   
    354.                     for ( var p in this.options.curAnim )   
    355.                         jQuery.attr(this.elem.style, p, this.options.orig[p]);   
    356.             }   
    357.   
    358.             if ( done )// 运行complete的回调函数   
    359.                 this.options.complete.call( this.elem );   
    360.   
    361.             return false;   
    362.         } else {   
    363.             var n = t - this.startTime;//时间间隔   
    364.             this.state = n / this.options.duration;//时间间隔比率   
    365.   
    366.             //根据时间间隔的比率再按一定的算法比率来计算当前的运动的位置点的比率。默认是swing的算法   
    367.             this.pos = jQuery.easing[this.options.easing ||    
    368.             (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 01this.options.duration);   
    369.             //当前的位置   
    370.             this.now = this.start + ((this.end - this.start) * this.pos);   
    371.   
    372.             // 显示   
    373.             this.update();   
    374.         }   
    375.   
    376.         return true;   
    377.     }   
    378.   
    379. };   
    380.   
    381. jQuery.extend( jQuery.fx, {   
    382.     // 动画的速度   
    383.     speeds:{   
    384.         slow: 600,   
    385.         fast: 200,   
    386.         // Default speed   
    387.         _default: 400  
    388.     },   
    389.        
    390.     // 为元素设值   
    391.     step: {   
    392.         opacity: function(fx){// 为元素CSS设opacity为fx   
    393.             jQuery.attr(fx.elem.style, "opacity", fx.now);   
    394.         },   
    395.   
    396.         _default: function(fx){   
    397.             if( fx.prop in fx.elem ) // 对于元素   
    398.                 fx.elem[ fx.prop ] = fx.now;   
    399.             else if( fx.elem.style )// 对于style   
    400.                 fx.elem.style[ fx.prop ] = fx.now + fx.unit;   
    401.         }   
    402.     }   
    403. });  
  • 相关阅读:
    【竞赛笔记】飞思卡尔智能车竞赛
    【实习笔记】智能广场健身设备总结
    RabbitMQ之安装
    数据结构与算法之队列
    joda-time时间操作组件
    JavaScript中的跨域问题
    数据结构与算法之链表
    Jedis集成到项目中
    ICMP协议和ping命令
    jedis的使用
  • 原文地址:https://www.cnblogs.com/rooney/p/1346474.html
Copyright © 2011-2022 走看看