jquery fx分析

引用:http://www.cnblogs.com/rooney/archive/2008/12/03/1346475.html


1. 8.1 FX的常用方法
2. 和前面分析的代码相比,FX是非常让人兴奋的。以前javaeye登陆的时候,对其登陆窗口的淡出淡入的特效总是想入非非。 Jquery的core包中也提供了Fx的实现。
3. Fx的实质是连续有序改变dom元素的属性达到视觉上的效果,动态地变化起来。这些属性主要是高度,宽度,透明度和颜色(背景色和前景色)等。连续有序是和时间相关的,也就是先在某个时间点改变一下CSS的样式属性,下一个时间点再改变一下样式属性,达到渐变的过程的效果。
4.
5. Jquery为我们提供了几种常用的FX的函数。
6. Slide是滑出的动作,对于slide,jquery提供了slideDown、slideUp、slideToggle 三种方法。slideDown是元素向下面渐渐地滑出,最后全部可见。slideUp是元素向上面渐渐地钻进去,最后不见。slideToggle则是这两者之间的转换了。
7. Fade是淡变的动作。对于Fade,jquery提供了fadeIn、fadeOut两个方法。fadeIn是从无到有渐渐地显示出整个元素。fadeOut相反,它从有到无渐渐地消失这个元素,fade还提供了一个fadeTo用来改变透明性渐变到一个指定的值,而不是消失。
8. Jquery还提供一组更强大的方法。Show、hide、toggle采用一种更为优美的方式显示元素。Show是元素的宽度渐渐变成,同时高度也渐渐变大,同时透明度也渐渐从无到不透明。这种的效果是元素从一个点渐渐变大,最终完全地显示出来。Hide刚是相反,它是渐渐透明同时宽高逐渐变小,最后消失。Toggle是两者之间的转换。
9.
10. jQuery.each({
11. slideDown: { height:"show" },
12. slideUp: { height: "hide" },
13. slideToggle: { height: "toggle" },
14. fadeIn: { opacity: "show" },
15. fadeOut: { opacity: "hide" }
16. }, function( name, props ){
17. jQuery.fn[ name ] = function( speed, callback ){
18. return this.animate( props, speed, callback );
19. };
20. });
21. // 把所有匹配元素的不透明度以渐进方式调整到指定的不透明度,
22. //并在动画完成后可选地触发一个回调函数。这个动画只调整元素的不透明度。
23. fadeTo: function(speed,to,callback){
24. return this.animate({opacity: to}, speed, callback);
25. },
26. Slide和fade是通过分别改变height或opacity来完成效果的。其完成的工作任务在this.animate( props, speed, callback ) 上。
27. show
28. 对于show和hide的,它们的效果更好一点,其代码也复杂一点。
29. // show(speed,[callback])
30. // 以优雅的动画隐藏所有匹配的元素,并在显示完成后可选地触发一个回调函数。
31. // 可以根据指定的速度动态地改变每个匹配元素的高度、宽度和不透明度。
32. // 显示隐藏的匹配元素 show()
33. show: function(speed,callback){
34. return speed ?
35. this.animate({height: "show", width: "show", opacity: "show"
36. }, speed, callback)
37. : this.filter(":hidden").each(function(){
38. this.style.display = this.oldblock || "";
39. if ( jQuery.css(this,"display") == "none" ) {
40. var elem = jQuery("<" + this.tagName + " />").appendTo("body");
41. this.style.display = elem.css("display");// 默认的显示的display
42. if (this.style.display == "none")// 显式地设定该tag不显示 this.style.display = "block";
43. elem.remove();// 上面这些的处理有没有必要呢?
44. }
45. }).end();// 回到前一个jQuery对象
46. },
47. hide: function(speed,callback){
48. return speed ?
49. this.animate({height: "hide", width: "hide", opacity: "hide"
50. }, speed, callback)
51. : this.filter(":visible").each(function(){
52. this.oldblock = this.oldblock || jQuery.css(this,"display");
53. this.style.display = "none";
54. }).end();
55. },
56. toggle: function( fn, fn2 ){
57. return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
58. this._toggle.apply( this, arguments ) :// 原来的toggle
59. (fn ? this.animate({height: "toggle", width: "toggle",
60. opacity: "toggle"}, fn, fn2)
61. : this.each(function(){jQuery(this)[ jQuery(this).is(":hidden") ?
62. "show" : "hide" ]();}
63. // 对每个元素都调用show,或hide函数。
64. )
65. );
66. },
67. show和hide的函数如果没有指定speed的参数,它们就直接地show或hide元素。没有动画的效果。如果给定的speed的参数。它能根据这个speed的值动态去改变高度宽度透明度来达到动画的show或hide的效果。Toggle则是这两样的变换。
68. prk/彭仁夔 转载请注明出处 <A href="http://jljlpch.iteye.com/">http://jljlpch.iteye.com/</A>

prk/彭仁夔 转载请注明出处 http://jljlpch.iteye.com/
8.1 FX的常用方法
和前面分析的代码相比,FX是非常让人兴奋的。以前javaeye登陆的时候,对其登陆窗口的淡出淡入的特效总是想入非非。Jquery的core包中也提供了Fx的实现。
Fx的实质是连续有序改变dom元素的属性达到视觉上的效果,动态地变化起来。这些属性主要是高度,宽度,透明度和颜色(背景色和前景色)等。连续有序是和时间相关的,也就是先在某个时间点改变一下CSS的样式属性,下一个时间点再改变一下样式属性,达到渐变的过程的效果。
Jquery为我们提供了几种常用的FX的函数。
Slide是滑出的动作,对于slide,jquery提供了slideDown、slideUp、slideToggle三种方法。slideDown是元素向下面渐渐地滑出,最后全部可见。slideUp是元素向上面渐渐地钻进去,最后不见。slideToggle则是这两者之间的转换了。
Fade是淡变的动作。对于Fade,jquery提供了fadeIn、fadeOut两个方法。fadeIn是从无到有渐渐地显示出整个元素。fadeOut相反,它从有到无渐渐地消失这个元素,fade还提供了一个fadeTo用来改变透明性渐变到一个指定的值,而不是消失。
Jquery还提供一组更强大的方法。Show、hide、toggle采用一种更为优美的方式显示元素。Show是元素的宽度渐渐变成,同时高度也渐渐变大,同时透明度也渐渐从无到不透明。这种的效果是元素从一个点渐渐变大,最终完全地显示出来。Hide刚是相反,它是渐渐透明同时宽高逐渐变小,最后消失。Toggle是两者之间的转换。
jQuery.each({
slideDown: { height:"show" },
slideUp: { height: "hide" },
slideToggle: { height: "toggle" },
fadeIn: { opacity: "show" },
fadeOut: { opacity: "hide" }
}, function( name, props ){
jQuery.fn[ name ] = function( speed, callback ){
return this.animate( props, speed, callback );
};
});
// 把所有匹配元素的不透明度以渐进方式调整到指定的不透明度,
//并在动画完成后可选地触发一个回调函数。这个动画只调整元素的不透明度。
fadeTo: function(speed,to,callback){
return this.animate({opacity: to}, speed, callback);
},
Slide和fade是通过分别改变height或opacity来完成效果的。其完成的工作任务在this.animate( props, speed, callback )上。
show
对于show和hide的,它们的效果更好一点,其代码也复杂一点。
// show(speed,[callback])
// 以优雅的动画隐藏所有匹配的元素,并在显示完成后可选地触发一个回调函数。
// 可以根据指定的速度动态地改变每个匹配元素的高度、宽度和不透明度。
// 显示隐藏的匹配元素 show()
show: function(speed,callback){
return speed ?
this.animate({height: "show", width: "show", opacity: "show"
}, speed, callback)
: this.filter(":hidden").each(function(){
this.style.display = this.oldblock || "";
if ( jQuery.css(this,"display") == "none" ) {
var elem = jQuery("<" + this.tagName + " />").appendTo("body");
this.style.display = elem.css("display");// 默认的显示的display
if (this.style.display == "none")// 显式地设定该tag不显示 this.style.display = "block";
elem.remove();// 上面这些的处理有没有必要呢?
}
}).end();// 回到前一个jQuery对象
},
hide: function(speed,callback){
return speed ?
this.animate({height: "hide", width: "hide", opacity: "hide"
}, speed, callback)
: this.filter(":visible").each(function(){
this.oldblock = this.oldblock || jQuery.css(this,"display");
this.style.display = "none";
}).end();
},
toggle: function( fn, fn2 ){
return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
this._toggle.apply( this, arguments ) :// 原来的toggle
(fn ? this.animate({height: "toggle", width: "toggle",
opacity: "toggle"}, fn, fn2)
: this.each(function(){jQuery(this)[ jQuery(this).is(":hidden") ?
"show" : "hide" ]();}
// 对每个元素都调用show,或hide函数。
)
);
},
show和hide的函数如果没有指定speed的参数,它们就直接地show或hide元素。没有动画的效果。如果给定的speed的参数。它能根据这个speed的值动态去改变高度宽度透明度来达到动画的show或hide的效果。Toggle则是这两样的变换。
prk/彭仁夔 转载请注明出处 http://jljlpch.iteye.com/

Java代码 复制代码

1. 8.2 Fx 的核心源码分析
2. 上面的几个常用方式都是调用了this.animate。jquery对象的animate是大包大揽的函数。所有的FX的效果都可以从这里得到。因为FX特效就是通过连续(间隔很小的时间点)去改变元素的CSS的样式。达到视觉上的效果。 Animate就是根据指定的参数(如speed,元素的属性的变化范围)来完成这样的功能。
3. Animate
4. /**
5. * 用于创建自定义动画的函数。
6. * 这个函数的关键在于指定动画形式及结果样式属性对象。这个对象中每个属性都表示一个可以变化的样式属性(如“height”、“top”或“opacity”)。
7. * 注意:所有指定的属性必须用骆驼形式,比如用marginLeft代替margin-left.
8. * 而每个属性的值表示这个样式属性到多少时动画结束。如果是一个数值,样式属性就会从当前的值渐变到指定的值。如果使用的是“hide”、“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。
9. * 在 jQuery 1.2 中,你可以使用 em 和 % 单位。另外,在 jQuery 1.2 中,你可以通过在属性值前面指定 "+=" 或
10. * "-=" 来让元素做相对运动。
11. *
12. * params (Options) : 一组包含作为动画属性和终值的样式属性和及其值的集合 。 duration (String,Number)
13. * :(可选) 三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000)
14. * easing (String) : (可选) 要使用的擦除效果的名称(需要插件支持).默认jQuery提供"linear" 和 "swing".
15. * callback (Function) : (可选) 在动画完成时执行的函数
16. */
17. animate: function( prop, speed, easing, callback ) {
18. var optall = jQuery.speed(speed, easing, callback); ①
19. // 执行each或queue方法
20. return this[ optall.queue === false ? "each" : "queue" ](function(){② var opt = jQuery.extend({}, optall), p,
21. hidden=this.nodeType==1&&jQuery(this).is(":hidden"),//元素节点且隐藏
22. self = this;// 当前的元素
23. for ( p in prop ) { ③
24. // 如果是完成的状态,就直接调用complate函数
25. if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) return opt.complete.call(this);
26. if (( p == "height" || p == "width")&& this.style ){//style高宽度
27. opt.display = jQuery.css(this, "display");// 保存当前元素的display
28. opt.overflow = this.style.overflow;// 保证没有暗中进行的
29. }
30. }
31. if ( opt.overflow != null )// 超出部分不见
32. this.style.overflow = "hidden";
33. opt.curAnim = jQuery.extend({}, prop);//clone传入的参数prop
34. jQuery.each( prop, function(name, val){ ④
35. // 对当前元素的给定的属性进行变化的操作
36. var e = new jQuery.fx( self, opt, name ); ⑤
37. // 传参的属性可以用 toggle,show,hide,其它
38. // 调用当前e.show,e.hide,e.val的方法,jQuery.fx.prototype
39. if ( /toggle|show|hide/.test(val) ) ⑥
40. e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop ); else { ⑦
41. var parts = al.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
42. start = e.cur(true) || 0;// 当前元素当前属性的值
43. // +=" 或 "-=" 来让元素做相对运动。
44. if ( parts ) { ⑧
45. var end = parseFloat(parts[2]), unit = parts[3] || "px"; if ( unit != "px" ) {// 计算开始的值=(end/cur)*start
46. self.style[ name ] = (end || 1) + unit;
47. start = ((end || 1) / e.cur(true)) * start;
48. self.style[ name ] = start + unit;
49. }
50. if( parts[1]) end = ((parts[1] == "-="? -1 : 1)* end)+ start;
51. e.custom( start, end, unit );// 动画
52. }
53. //直接计算start和 end的位置来动画。val是数值的end,start当前的属性值。
54. else e.custom( start, val, "" );// 动画 ⑨
55. }
56. });
57. / For JS strict compliance
58. return true;
59. }); },
60.
61. Animate通过传入的参数对于jquery对象中的每个元素的每个指示的属性都进行时间上渐变的改变。下面就分析其中的代码。
62. jQuery.speed
63. 在①是就通过jquery.speed来进行参数的统一整理。
64. // 主要用于辅助性的工作
65. speed: function(speed, easing, fn) {
66. var opt = speed && speed.constructor == Object ? speed : {
67. // coplete是至多三个参数的最后一个。看看是否传入动画完成回调的函数
68. complete: fn ||!fn && easing ||jQuery.isFunction( speed ) && speed,
69. duration: speed,// 持继的时间,动画运行的时间。
70. //找到动画中属性随时间渐变的的算法。
71. easing:fn&&easing || easing && easing.constructor != Function && easing
72. };
73. //计算出正确的duration,它支持 fast,slow这样已经定义的常量
74. opt.duration = (opt.duration &&
75. (opt.duration.constructor == Number?
76. opt.duration :
77. jQuery.fx.speeds[opt.duration]))||jQuery.fx.speeds._default;
78. // Queueing
79. opt.old = opt.complete;
80. //这里是把complete形成了包裹,看看参数中是否指定队列操作,
81. //如果先出列,再执行complete
82. opt.complete = function(){
83. //可能通过参数指定queue,this 指向是当前的dom元素。
84. if ( opt.queue !== false ) jQuery(this).dequeue();//出queue
85. if (jQuery.isFunction( opt.old )) opt.old.call( this );
86. };
87.
88. return opt;
89. },
90. jquery.speed是对参数进行管理的操作函数。它首先看看speed是不是紧缩型的参数如 { complete:xx, easing:xx, duration:xx}。如果不是,就根据传入的参数进行判断,组成紧缩型的对象参数。
91. 由于jquery对象支持fast,slow这样的常量来代替具体的speed的数值,第二步就是进行speed的处理。如果没有提供就提供默认的speed。其代码在jQuery.fx.speeds中speeds:{ slow: 600, fast: 200, _default: 400},定义了三种形式的速度。
92. 接下来是对在完成的时间执行的complete的回调函数进行包裹。包裹就是看看参数中提供了队列的操作没有。提供了就进行出队列的操作。之后再执行complete的回调函数。
93. jQuery.fn.dequeue = function(type){
94. type = type || "fx";
95. return this.each(function(){
96. var q = queue(this, type);// 得取type的的值
97. q.shift();// 移出一个
98. if ( q.length )
99. q[0].call( this );
100. });
101. };
102. jQuery.fn.dequeue根据type取到当前dom元素的queue。先移出一个。再运行queue中的第一个。
103. Queue

8.2 Fx的核心源码分析
上面的几个常用方式都是调用了this.animate。jquery对象的animate是大包大揽的函数。所有的FX的效果都可以从这里得到。因为FX特效就是通过连续(间隔很小的时间点)去改变元素的CSS的样式。达到视觉上的效果。Animate就是根据指定的参数(如speed,元素的属性的变化范围)来完成这样的功能。
Animate
/**
* 用于创建自定义动画的函数。
* 这个函数的关键在于指定动画形式及结果样式属性对象。这个对象中每个属性都表示一个可以变化的样式属性(如“height”、“top”或“opacity”)。
* 注意:所有指定的属性必须用骆驼形式,比如用marginLeft代替margin-left.
* 而每个属性的值表示这个样式属性到多少时动画结束。如果是一个数值,样式属性就会从当前的值渐变到指定的值。如果使用的是“hide”、“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。
* 在 jQuery 1.2 中,你可以使用 em 和 % 单位。另外,在 jQuery 1.2 中,你可以通过在属性值前面指定 "+=" 或
* "-=" 来让元素做相对运动。
*
* params (Options) : 一组包含作为动画属性和终值的样式属性和及其值的集合 。 duration (String,Number)
* :(可选) 三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000)
* easing (String) : (可选) 要使用的擦除效果的名称(需要插件支持).默认jQuery提供"linear" 和 "swing".
* callback (Function) : (可选) 在动画完成时执行的函数
*/
animate: function( prop, speed, easing, callback ) {
var optall = jQuery.speed(speed, easing, callback); ①
// 执行each或queue方法
return this[ optall.queue === false ? "each" : "queue" ](function(){② var opt = jQuery.extend({}, optall), p,
hidden=this.nodeType==1&&jQuery(this).is(":hidden"),//元素节点且隐藏
self = this;// 当前的元素
for ( p in prop ) { ③
//如果是完成的状态,就直接调用complate函数
if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) return opt.complete.call(this);
if (( p == "height" || p == "width")&& this.style ){//style高宽度
opt.display = jQuery.css(this, "display");// 保存当前元素的display
opt.overflow = this.style.overflow;// 保证没有暗中进行的
}
}
if ( opt.overflow != null )// 超出部分不见
this.style.overflow = "hidden";
opt.curAnim = jQuery.extend({}, prop);//clone传入的参数prop
jQuery.each( prop, function(name, val){ ④
// 对当前元素的给定的属性进行变化的操作
var e = new jQuery.fx( self, opt, name ); ⑤
// 传参的属性可以用toggle,show,hide,其它
// 调用当前e.show,e.hide,e.val的方法,jQuery.fx.prototype
if ( /toggle|show|hide/.test(val) ) ⑥
e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop ); else { ⑦
var parts = al.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
start = e.cur(true) || 0;// 当前元素当前属性的值
// +=" 或 "-=" 来让元素做相对运动。
if ( parts ) { ⑧
var end = parseFloat(parts[2]), unit = parts[3] || "px"; if ( unit != "px" ) {// 计算开始的值=(end/cur)*start
self.style[ name ] = (end || 1) + unit;
start = ((end || 1) / e.cur(true)) * start;
self.style[ name ] = start + unit;
}
if( parts[1]) end = ((parts[1] == "-="? -1 : 1)* end)+ start;
e.custom( start, end, unit );// 动画
}
//直接计算start和end的位置来动画。val是数值的end,start当前的属性值。
else e.custom( start, val, "" );// 动画 ⑨
}
});
/ For JS strict compliance
return true;
}); },
Animate通过传入的参数对于jquery对象中的每个元素的每个指示的属性都进行时间上渐变的改变。下面就分析其中的代码。
jQuery.speed
在①是就通过jquery.speed来进行参数的统一整理。
// 主要用于辅助性的工作
speed: function(speed, easing, fn) {
var opt = speed && speed.constructor == Object ? speed : {
// coplete是至多三个参数的最后一个。看看是否传入动画完成回调的函数
complete: fn ||!fn && easing ||jQuery.isFunction( speed ) && speed,
duration: speed,// 持继的时间,动画运行的时间。
//找到动画中属性随时间渐变的的算法。
easing:fn&&easing || easing && easing.constructor != Function && easing
};
//计算出正确的duration,它支持fast,slow这样已经定义的常量
opt.duration = (opt.duration &&
(opt.duration.constructor == Number?
opt.duration :
jQuery.fx.speeds[opt.duration]))||jQuery.fx.speeds._default;
// Queueing
opt.old = opt.complete;
//这里是把complete形成了包裹,看看参数中是否指定队列操作,
//如果先出列,再执行complete
opt.complete = function(){
//可能通过参数指定queue,this指向是当前的dom元素。
if ( opt.queue !== false ) jQuery(this).dequeue();//出queue
if (jQuery.isFunction( opt.old )) opt.old.call( this );
};
return opt;
},
jquery.speed是对参数进行管理的操作函数。它首先看看speed是不是紧缩型的参数如{ complete:xx, easing:xx, duration:xx}。如果不是,就根据传入的参数进行判断,组成紧缩型的对象参数。
由于jquery对象支持fast,slow这样的常量来代替具体的speed的数值,第二步就是进行speed的处理。如果没有提供就提供默认的speed。其代码在jQuery.fx.speeds中speeds:{ slow: 600, fast: 200, _default: 400},定义了三种形式的速度。
接下来是对在完成的时间执行的complete的回调函数进行包裹。包裹就是看看参数中提供了队列的操作没有。提供了就进行出队列的操作。之后再执行complete的回调函数。
jQuery.fn.dequeue = function(type){
type = type || "fx";
return this.each(function(){
var q = queue(this, type);// 得取type的的值
q.shift();// 移出一个
if ( q.length )
q[0].call( this );
});
};
jQuery.fn.dequeue根据type取到当前dom元素的queue。先移出一个。再运行queue中的第一个。
Queue

Java代码 复制代码

1. prk/彭仁夔 转载请注明出处 <A href="http://jljlpch.iteye.com/">http://jljlpch.iteye.com/</A>
2.
3. 在②处和上一部分都看看参数中提供了队列的操作没有,有就是进行queue的操作。Queue的操作和each的操作是不一样的。
4. // 实现队列操作,为jQuery对象中的每个元素都加type的属性,值为fn.
5. queue: function(type, fn){
6. // 可能看出支持一个参数的fn 或array形式的集合其type为默认的fx的形式。
7. if(jQuery.isFunction(type)||( type && type.constructor == Array)) {
8. fn = type; type = "fx"; }
9. //没有参数时或一个参数是字符的type时就从jquery对象第一个元素的data中取
10. //出相对于的type(空就是所有)的队列中的元素(fn)。
11. if ( !type || (typeof type == "string" && !fn) )
12. return queue( this[0], type );//从queue取出
13. //把fn保存在jquery对象中的每个元素的data中的对应的type中去。
14. //对于fn是fn的数组集合,就直接设定 data中type为fn的数组
15. //如果是单个的fn,那么就采用追加的形式追加到data的type的fn数组中。
16. //如果追加的形式,而且之前数组中没有 fn元素。那么就执行这个元素。
17. return this.each(function(){
18. if ( fn.constructor == Array )// 数组的形式
19. queue(this, type, fn);// 存储在元素的type属性中
20. else {
21. queue(this, type).push( fn );
22. if ( queue(this, type).length == 1 ) fn.call(this);
23. }
24. });
25. },
26. 分析上面的代码可以看出Queue的操作和each的操作是不太一样的。Each是对每个元素都执行fn函数。而queue对于每个元素都把fn函数放到自已对应的cache中fx的属性中保存,是单个的fn的话,也立马运行。在②处和each的作用差不多。
27. 在上面的代码,它还调用了queue(this, type, fn);来实现把把fn存到this元素的data中的对应的type中去。
28. // 为元素加上type的array的属性,或返回取到elem上type的值
29. var queue = function( elem, type, array ) {
30. if ( elem ){
31. type = type || "fx";
32. var q = jQuery.data( elem, type + "queue" );
33. if ( !q || array )
34. q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) );
35. }
36. return q;
37. };
38. 这个全局的函数就是在jQuery.data进一步封装,使它支持默认的fx type和array的类数组的参数。
39. Jquery这里的Queue实质上没有起到什么作用。它本来可以支持一个元素的的连续执行几个Queue的函数达到更丰富的动画的效果。估计这里是没有完成。
40. ①②③④⑤⑥⑦⑧⑨⑩
41. jQuery.fx
42. ⑤处animate为dom元素的每个指定的属性都生成了一个fx对象。
43. // 根据参数构成一个对象
44. fx: function( elem, options, prop ){
45. this.options = options;
46. this.elem = elem;
47. this.prop = prop;
48.
49. if ( !options.orig )
50. options.orig = {};
51. }
52. Fx函数是一个构建函数,仅仅是把参数变成本对象的属性。以便于fx对象的其它方法来使用这些参数。在⑥处就是通过调用fx 的方法show或hidden来完成一个属性的逐渐的改变。在⑨是通过custom方法来直接进行一个动画。
53. 先看一下show或hidden:
54. show: function(){
55. // 保存当前的,以被修改之后能得到初始的值
56. this.options.orig[this.prop]=jQuery.attr(this.elem.style,this.prop);
57. this.options.show = true;//标明是进行show操作
58. this.custom(0, this.cur());
59. //让最开始时以1px的宽或高度来显示。防止内容flash
60. if ( this.prop == "width" || this.prop == "height" )
61. this.elem.style[this.prop] = "1px";
62. jQuery(this.elem).show();
63. },
64. hide: function(){
65. // 保存当前的,以被修改之后能得到初始的值
66. this.options.orig[this.prop]=jQuery.attr(this.elem.style,this.prop);
67. this.options.hide = true;//标识是进行hide操作
68. this.custom(this.cur(), 0); },
69. show和hide是在指定元素的属性为show或hide的时候调用的,如height: "show", width: "show", opacity: "show"。它们都是先保存原始的改悔。之后调用custom来完成动画。和⑨处是一样的。
70. 也就是说完成动画的工作在custom中:
71. // 开动一个动画
72. custom: function(from, to, unit){
73. this.startTime = now();//动画开始的时候
74. this.start = from;//位置开始点
75. this.end = to;//位置结果点
76. this.unit = unit || this.unit || "px";
77. this.now = this.start;//位置当前点
78. //state 是时间间隔在总的duration的比率
79. //pos 是按一定算法把时间上的比率折算到位置上的比率
80. this.pos = this.state = 0;
81. // 根据this.now位置当前点的值来设定元素的属性显示出来
82. this.update();
83.
84. var self = this;
85. function t(gotoEnd){
86. return self.step(gotoEnd);// 调用step(gotoEnd)//本对象的
87. }
88. t.elem = this.elem;//删除的时候做判断用
89. //timers 栈是公共的,不同的元素的不同的属性step都是放在这里面。
90. jQuery.timers.push(t);
91.
92. if ( jQuery.timerId == null ) {
93. jQuery.timerId = setInterval(function(){
94. var timers = jQuery.timers;
95. //倒是觉得这里会有同步冲突的问题。Ext.observable中就有解决方法
96. for ( var i = 0; i < timers.length; i++ )
97. //当一个属性的动画完成,或强迫完成的时候,把step从数组中删除.
98. //同时把i的位置不改变。继续下一个。
99. if ( !timers[i]() ) timers.splice(i--, 1);
100. //说明还有属性的动画没有完成,step还在timers中。
101. //那么就不clearInterval,13ms之后再继续。直到数组
102. //中所有的step都被删除。
103. if ( !timers.length ) {
104. clearInterval( jQuery.timerId );
105. jQuery.timerId = null;
106. }
107. }, 13);
108. }
109. },
110. 在custom中为fx对象动态地追加了几个属性。Start和end属性指的属性值变化的开始和结束位置。这是属性值发生变化的范围。Now是在Start和end 范围的某一个点,也就是当前要比属性设定值。这个值是根据时间的间隔比率再通过一定的算法来得出的。 pos 和state一个是位置上的比率,一个是时间上比率,值在0~1之间。
111. Custom通过this.now = this.start;和this.update(); 来设起始位置的样式属性。
112. // 为元素设值,更新显示
113. update: function(){
114. //可以在显示之前进行自定义的显示操作
115. // 这里可以是改变this.now或元素的其它属性。
116. // 改变this.now是改变动画的轨迹,改变其它的属性会有更多的效果
117. if ( this.options.step )
118. this.options.step.call( this.elem, this.now, this );
119. //根据this.now来改变/设值当前属性的值。也就改变了样式。
120. (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
121.
122. // 对于高度和宽度,肯定是要能看出效果的,故采用display=block。
123. if ( ( this.prop == "height" || this.prop == "width" )
124. && this.elem.style )
125. this.elem.style.display = "block";
126. },

prk/彭仁夔 转载请注明出处 http://jljlpch.iteye.com/
在②处和上一部分都看看参数中提供了队列的操作没有,有就是进行queue的操作。Queue的操作和each的操作是不一样的。
// 实现队列操作,为jQuery对象中的每个元素都加type的属性,值为fn.
queue: function(type, fn){
// 可能看出支持一个参数的fn或array形式的集合其type为默认的fx的形式。
if(jQuery.isFunction(type)||( type && type.constructor == Array)) {
fn = type; type = "fx"; }
//没有参数时或一个参数是字符的type时就从jquery对象第一个元素的data中取
//出相对于的type(空就是所有)的队列中的元素(fn)。
if ( !type || (typeof type == "string" && !fn) )
return queue( this[0], type );//从queue取出
//把fn保存在jquery对象中的每个元素的data中的对应的type中去。
//对于fn是fn的数组集合,就直接设定data中type为fn的数组
//如果是单个的fn,那么就采用追加的形式追加到data的type的fn数组中。
//如果追加的形式,而且之前数组中没有fn元素。那么就执行这个元素。
return this.each(function(){
if ( fn.constructor == Array )// 数组的形式
queue(this, type, fn);// 存储在元素的type属性中
else {
queue(this, type).push( fn );
if ( queue(this, type).length == 1 ) fn.call(this);
}
});
},
分析上面的代码可以看出Queue的操作和each的操作是不太一样的。Each是对每个元素都执行fn函数。而queue对于每个元素都把fn函数放到自已对应的cache中fx的属性中保存,是单个的fn的话,也立马运行。在②处和each的作用差不多。
在上面的代码,它还调用了queue(this, type, fn);来实现把把fn存到this元素的data中的对应的type中去。
// 为元素加上type的array的属性,或返回取到elem上type的值
var queue = function( elem, type, array ) {
if ( elem ){
type = type || "fx";
var q = jQuery.data( elem, type + "queue" );
if ( !q || array )
q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) );
}
return q;
};
这个全局的函数就是在jQuery.data进一步封装,使它支持默认的fx type和array的类数组的参数。
Jquery这里的Queue实质上没有起到什么作用。它本来可以支持一个元素的的连续执行几个Queue的函数达到更丰富的动画的效果。估计这里是没有完成。
①②③④⑤⑥⑦⑧⑨⑩
jQuery.fx
⑤处animate为dom元素的每个指定的属性都生成了一个fx对象。
// 根据参数构成一个对象
fx: function( elem, options, prop ){
this.options = options;
this.elem = elem;
this.prop = prop;
if ( !options.orig )
options.orig = {};
}
Fx函数是一个构建函数,仅仅是把参数变成本对象的属性。以便于fx对象的其它方法来使用这些参数。在⑥处就是通过调用fx的方法show或hidden来完成一个属性的逐渐的改变。在⑨是通过custom方法来直接进行一个动画。
先看一下show或hidden:
show: function(){
// 保存当前的,以被修改之后能得到初始的值
this.options.orig[this.prop]=jQuery.attr(this.elem.style,this.prop);
this.options.show = true;//标明是进行show操作
this.custom(0, this.cur());
//让最开始时以1px的宽或高度来显示。防止内容flash
if ( this.prop == "width" || this.prop == "height" )
this.elem.style[this.prop] = "1px";
jQuery(this.elem).show();
},
hide: function(){
// 保存当前的,以被修改之后能得到初始的值
this.options.orig[this.prop]=jQuery.attr(this.elem.style,this.prop);
this.options.hide = true;//标识是进行hide操作
this.custom(this.cur(), 0); },
show和hide是在指定元素的属性为show或hide的时候调用的,如height: "show", width: "show", opacity: "show"。它们都是先保存原始的改悔。之后调用custom来完成动画。和⑨处是一样的。
也就是说完成动画的工作在custom中:
// 开动一个动画
custom: function(from, to, unit){
this.startTime = now();//动画开始的时候
this.start = from;//位置开始点
this.end = to;//位置结果点
this.unit = unit || this.unit || "px";
this.now = this.start;//位置当前点
//state是时间间隔在总的duration的比率
//pos是按一定算法把时间上的比率折算到位置上的比率
this.pos = this.state = 0;
//根据this.now位置当前点的值来设定元素的属性显示出来
this.update();
var self = this;
function t(gotoEnd){
return self.step(gotoEnd);// 调用step(gotoEnd)//本对象的
}
t.elem = this.elem;//删除的时候做判断用
//timers栈是公共的,不同的元素的不同的属性step都是放在这里面。
jQuery.timers.push(t);
if ( jQuery.timerId == null ) {
jQuery.timerId = setInterval(function(){
var timers = jQuery.timers;
//倒是觉得这里会有同步冲突的问题。Ext.observable中就有解决方法
for ( var i = 0; i < timers.length; i++ )
//当一个属性的动画完成,或强迫完成的时候,把step从数组中删除.
//同时把i的位置不改变。继续下一个。
if ( !timers[i]() ) timers.splice(i--, 1);
//说明还有属性的动画没有完成,step还在timers中。
//那么就不clearInterval,13ms之后再继续。直到数组
//中所有的step都被删除。
if ( !timers.length ) {
clearInterval( jQuery.timerId );
jQuery.timerId = null;
}
}, 13);
}
},
在custom中为fx对象动态地追加了几个属性。Start和end属性指的属性值变化的开始和结束位置。这是属性值发生变化的范围。Now是在Start和end 范围的某一个点,也就是当前要比属性设定值。这个值是根据时间的间隔比率再通过一定的算法来得出的。pos 和state一个是位置上的比率,一个是时间上比率,值在0~1之间。
Custom通过this.now = this.start;和this.update();来设起始位置的样式属性。
// 为元素设值,更新显示
update: function(){
//可以在显示之前进行自定义的显示操作
//这里可以是改变this.now或元素的其它属性。
//改变this.now是改变动画的轨迹,改变其它的属性会有更多的效果
if ( this.options.step )
this.options.step.call( this.elem, this.now, this );
//根据this.now来改变/设值当前属性的值。也就改变了样式。
(jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
// 对于高度和宽度,肯定是要能看出效果的,故采用display=block。
if ( ( this.prop == "height" || this.prop == "width" )
&& this.elem.style )
this.elem.style.display = "block";
},

Java代码 复制代码

1. prk/彭仁夔 转载请注明出处 <A href="http://jljlpch.iteye.com/">http://jljlpch.iteye.com/</A>
2.
3. 通过udate设定起始位置的样式属性之后,custom第二步就是采用setInterval
4. 每隔13ms就执行一次当前的fx对象的step方法。该方法实现了动画结束的扫尾工作和动画过程中按一定的算法来计算this.now 的值。因为这个值的改变,之后调用update就可以改变样式的属性。
5. 这样一来,就实现了元素的某个属性的渐变过程。
6. // 动画的每一个步骤
7. step: function(gotoEnd){
8. var t = now();//运行到当前的时间,因为是13ms才运行一次。
9. // 强行指定结束或当前时间大于startTime+duration
10. if ( gotoEnd || t > this.options.duration + this.startTime ) {
11. this.now = this.end;//当前的位置为结束位置
12. this.pos = this.state = 1;//当前的state,pos的比率为1.最大。
13. this.update();//显示
14. //标识这个属性的动画已经完成
15. this.options.curAnim[ this.prop ] = true;
16. //再一次判断是否完成
17. var done = true;
18. for ( var i in this.options.curAnim )
19. if ( this.options.curAnim[i] !== true )
20. done = false;
21.
22. if ( done ) {
23. if ( this.options.display != null ) {// 恢复overflow
24. this.elem.style.overflow = this.options.overflow;
25. // 恢复 display
26. this.elem.style.display = this.options.display;
27. //判断其是否恢复成功,
28. if ( jQuery.css(this.elem, "display") == "none" )
29. this.elem.style.display = "block";
30. }
31.
32. //如果是hide的操作
33. if ( this.options.hide )
34. this.elem.style.display = "none";
35.
36. // 如果元素已经show或hide,恢复其动画改变的属性
37. if ( this.options.hide || this.options.show )
38. for ( var p in this.options.curAnim )
39. jQuery.attr(this.elem.style, p,
40. this.options.orig[p]);
41. }
42.
43. if ( done )// 运行complete的回调函数
44. this.options.complete.call( this.elem );
45.
46. return false;
47. } else {
48. var n = t - this.startTime;//时间间隔
49. this.state = n / this.options.duration;//时间间隔比率
50.
51. //根据时间间隔的比率再按一定的算法比率来计算
52. //当前的运动的位置点的比率。默认是 swing的算法
53. this.pos = jQuery.easing[this.options.easing ||
54. (jQuery.easing.swing ? "swing" : "linear")]
55. (this.state, n, 0, 1, this.options.duration);
56. //当前的位置
57. this.now = this.start + ((this.end - this.start) * this.pos);
58.
59. // 显示
60. this.update();
61. }
62.
63. return true;
64. }
65. Step中第一部分是完成时的扫尾工作,恢复动画时改变的属性和运行complete的回调函数。第二部分就是计算this.now 的值之后显示出来。目前jquery提供了两种算法来计算从时间间隔比率转换成位置上的比率。
66. easing: {
67. linear: function( p, n, firstNum, diff ) {
68. return firstNum + diff * p;
69. },
70. swing: function( p, n, firstNum, diff ) {
71. return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
72. }
73. },
74. 这就是它的算法。对于linear的形式,Jquery采用了1:1的关系来计算的。
75. Stop
76. 一个动画,有的时间可能会出现在没有运行完成就中断。Stop就是对这个进行操作的。
77. stop: function(clearQueue, gotoEnd){
78. var timers = jQuery.timers;
79. if (clearQueue) this.queue([]);// 清除
80. this.each(function(){
81. // 倒序是为了能把在loop过程加入timers的当前元素的属性的动画step也给删除。
82. for ( var i = timers.length - 1; i >= 0; i-- )
83. if ( timers[i].elem == this ) {
84. if (gotoEnd) timers[i](true); // 强迫动画结束
85. timers.splice(i, 1);
86. }
87. });
88. // start the next in the queue if the last step wasn't forced
89. if (!gotoEnd) this.dequeue();
90.
91. return this;
92. }
93. 在stop中,我们可以看出step(force)中的参数的作用。这个强迫动画到最后一步,即动画结束进行扫尾工作。这里的 jQuery.timers是公共的集合。在custom中的setInterval就是对它进行操作的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值