PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jquery/src/fx.js

https://github.com/deamon12208/jquery
JavaScript | 413 lines | 292 code | 87 blank | 34 comment | 120 complexity | 79d940d8fc20de17e418db0677caffcb MD5 | raw file
Possible License(s): LGPL-2.1, MIT
  1. jQuery.fn.extend({
  2. show: function(speed,callback){
  3. return speed ?
  4. this.animate({
  5. height: "show", width: "show", opacity: "show"
  6. }, speed, callback) :
  7. this.filter(":hidden").each(function(){
  8. this.style.display = this.oldblock || "";
  9. if ( jQuery.css(this,"display") == "none" ) {
  10. var elem = jQuery("<" + this.tagName + " />").appendTo("body");
  11. this.style.display = elem.css("display");
  12. // handle an edge condition where css is - div { display:none; } or similar
  13. if (this.style.display == "none")
  14. this.style.display = "block";
  15. elem.remove();
  16. }
  17. }).end();
  18. },
  19. hide: function(speed,callback){
  20. return speed ?
  21. this.animate({
  22. height: "hide", width: "hide", opacity: "hide"
  23. }, speed, callback) :
  24. this.filter(":visible").each(function(){
  25. this.oldblock = this.oldblock || jQuery.css(this,"display");
  26. this.style.display = "none";
  27. }).end();
  28. },
  29. // Save the old toggle function
  30. _toggle: jQuery.fn.toggle,
  31. toggle: function( fn, fn2 ){
  32. return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
  33. this._toggle.apply( this, arguments ) :
  34. fn ?
  35. this.animate({
  36. height: "toggle", width: "toggle", opacity: "toggle"
  37. }, fn, fn2) :
  38. this.each(function(){
  39. jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
  40. });
  41. },
  42. fadeTo: function(speed,to,callback){
  43. return this.animate({opacity: to}, speed, callback);
  44. },
  45. animate: function( prop, speed, easing, callback ) {
  46. var optall = jQuery.speed(speed, easing, callback);
  47. return this[ optall.queue === false ? "each" : "queue" ](function(){
  48. var opt = jQuery.extend({}, optall), p,
  49. hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),
  50. self = this;
  51. for ( p in prop ) {
  52. if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
  53. return opt.complete.call(this);
  54. if ( ( p == "height" || p == "width" ) && this.style ) {
  55. // Store display property
  56. opt.display = jQuery.css(this, "display");
  57. // Make sure that nothing sneaks out
  58. opt.overflow = this.style.overflow;
  59. }
  60. }
  61. if ( opt.overflow != null )
  62. this.style.overflow = "hidden";
  63. opt.curAnim = jQuery.extend({}, prop);
  64. jQuery.each( prop, function(name, val){
  65. var e = new jQuery.fx( self, opt, name );
  66. if ( /toggle|show|hide/.test(val) )
  67. e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
  68. else {
  69. var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
  70. start = e.cur(true) || 0;
  71. if ( parts ) {
  72. var end = parseFloat(parts[2]),
  73. unit = parts[3] || "px";
  74. // We need to compute starting value
  75. if ( unit != "px" ) {
  76. self.style[ name ] = (end || 1) + unit;
  77. start = ((end || 1) / e.cur(true)) * start;
  78. self.style[ name ] = start + unit;
  79. }
  80. // If a +=/-= token was provided, we're doing a relative animation
  81. if ( parts[1] )
  82. end = ((parts[1] == "-=" ? -1 : 1) * end) + start;
  83. e.custom( start, end, unit );
  84. } else
  85. e.custom( start, val, "" );
  86. }
  87. });
  88. // For JS strict compliance
  89. return true;
  90. });
  91. },
  92. queue: function(type, fn){
  93. if ( jQuery.isFunction(type) || ( type && type.constructor == Array )) {
  94. fn = type;
  95. type = "fx";
  96. }
  97. if ( !type || (typeof type == "string" && !fn) )
  98. return queue( this[0], type );
  99. return this.each(function(){
  100. if ( fn.constructor == Array )
  101. queue(this, type, fn);
  102. else {
  103. queue(this, type).push( fn );
  104. if ( queue(this, type).length == 1 )
  105. fn.call(this);
  106. }
  107. });
  108. },
  109. stop: function(clearQueue, gotoEnd){
  110. var timers = jQuery.timers;
  111. if (clearQueue)
  112. this.queue([]);
  113. this.each(function(){
  114. // go in reverse order so anything added to the queue during the loop is ignored
  115. for ( var i = timers.length - 1; i >= 0; i-- )
  116. if ( timers[i].elem == this ) {
  117. if (gotoEnd)
  118. // force the next step to be the last
  119. timers[i](true);
  120. timers.splice(i, 1);
  121. }
  122. });
  123. // start the next in the queue if the last step wasn't forced
  124. if (!gotoEnd)
  125. this.dequeue();
  126. return this;
  127. }
  128. });
  129. // Generate shortcuts for custom animations
  130. jQuery.each({
  131. slideDown: { height:"show" },
  132. slideUp: { height: "hide" },
  133. slideToggle: { height: "toggle" },
  134. fadeIn: { opacity: "show" },
  135. fadeOut: { opacity: "hide" }
  136. }, function( name, props ){
  137. jQuery.fn[ name ] = function( speed, callback ){
  138. return this.animate( props, speed, callback );
  139. };
  140. });
  141. var queue = function( elem, type, array ) {
  142. if ( elem ){
  143. type = type || "fx";
  144. var q = jQuery.data( elem, type + "queue" );
  145. if ( !q || array )
  146. q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) );
  147. }
  148. return q;
  149. };
  150. jQuery.fn.dequeue = function(type){
  151. type = type || "fx";
  152. return this.each(function(){
  153. var q = queue(this, type);
  154. q.shift();
  155. if ( q.length )
  156. q[0].call( this );
  157. });
  158. };
  159. jQuery.extend({
  160. speed: function(speed, easing, fn) {
  161. var opt = speed && speed.constructor == Object ? speed : {
  162. complete: fn || !fn && easing ||
  163. jQuery.isFunction( speed ) && speed,
  164. duration: speed,
  165. easing: fn && easing || easing && easing.constructor != Function && easing
  166. };
  167. opt.duration = typeof opt.duration == 'number' ? opt.duration :
  168. jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
  169. // Queueing
  170. opt.old = opt.complete;
  171. opt.complete = function(){
  172. if ( opt.queue !== false )
  173. jQuery(this).dequeue();
  174. if ( jQuery.isFunction( opt.old ) )
  175. opt.old.call( this );
  176. };
  177. return opt;
  178. },
  179. easing: {
  180. linear: function( p, n, firstNum, diff ) {
  181. return firstNum + diff * p;
  182. },
  183. swing: function( p, n, firstNum, diff ) {
  184. return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
  185. }
  186. },
  187. timers: [],
  188. timerId: null,
  189. fx: function( elem, options, prop ){
  190. this.options = options;
  191. this.elem = elem;
  192. this.prop = prop;
  193. if ( !options.orig )
  194. options.orig = {};
  195. }
  196. });
  197. jQuery.fx.prototype = {
  198. // Simple function for setting a style value
  199. update: function(){
  200. if ( this.options.step )
  201. this.options.step.call( this.elem, this.now, this );
  202. (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
  203. // Set display property to block for height/width animations
  204. if ( ( this.prop == "height" || this.prop == "width" ) && this.elem.style )
  205. this.elem.style.display = "block";
  206. },
  207. // Get the current size
  208. cur: function(force){
  209. if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) )
  210. return this.elem[ this.prop ];
  211. var r = parseFloat(jQuery.css(this.elem, this.prop, force));
  212. return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
  213. },
  214. // Start an animation from one number to another
  215. custom: function(from, to, unit){
  216. this.startTime = now();
  217. this.start = from;
  218. this.end = to;
  219. this.unit = unit || this.unit || "px";
  220. this.now = this.start;
  221. this.pos = this.state = 0;
  222. var self = this;
  223. function t(gotoEnd){
  224. return self.step(gotoEnd);
  225. }
  226. t.elem = this.elem;
  227. jQuery.timers.push(t);
  228. if ( t() && jQuery.timerId == null ) {
  229. jQuery.timerId = setInterval(function(){
  230. var timers = jQuery.timers;
  231. for ( var i = 0; i < timers.length; i++ )
  232. if ( !timers[i]() )
  233. timers.splice(i--, 1);
  234. if ( !timers.length ) {
  235. clearInterval( jQuery.timerId );
  236. jQuery.timerId = null;
  237. }
  238. }, 13);
  239. }
  240. },
  241. // Simple 'show' function
  242. show: function(){
  243. // Remember where we started, so that we can go back to it later
  244. this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
  245. this.options.show = true;
  246. // Begin the animation
  247. this.custom(0, this.cur());
  248. // Make sure that we start at a small width/height to avoid any
  249. // flash of content
  250. if ( this.prop == "width" || this.prop == "height" )
  251. this.elem.style[this.prop] = "1px";
  252. // Start by showing the element
  253. jQuery(this.elem).show();
  254. },
  255. // Simple 'hide' function
  256. hide: function(){
  257. // Remember where we started, so that we can go back to it later
  258. this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
  259. this.options.hide = true;
  260. // Begin the animation
  261. this.custom(this.cur(), 0);
  262. },
  263. // Each step of an animation
  264. step: function(gotoEnd){
  265. var t = now();
  266. if ( gotoEnd || t >= this.options.duration + this.startTime ) {
  267. this.now = this.end;
  268. this.pos = this.state = 1;
  269. this.update();
  270. this.options.curAnim[ this.prop ] = true;
  271. var done = true;
  272. for ( var i in this.options.curAnim )
  273. if ( this.options.curAnim[i] !== true )
  274. done = false;
  275. if ( done ) {
  276. if ( this.options.display != null ) {
  277. // Reset the overflow
  278. this.elem.style.overflow = this.options.overflow;
  279. // Reset the display
  280. this.elem.style.display = this.options.display;
  281. if ( jQuery.css(this.elem, "display") == "none" )
  282. this.elem.style.display = "block";
  283. }
  284. // Hide the element if the "hide" operation was done
  285. if ( this.options.hide )
  286. this.elem.style.display = "none";
  287. // Reset the properties, if the item has been hidden or shown
  288. if ( this.options.hide || this.options.show )
  289. for ( var p in this.options.curAnim )
  290. jQuery.attr(this.elem.style, p, this.options.orig[p]);
  291. }
  292. if ( done )
  293. // Execute the complete function
  294. this.options.complete.call( this.elem );
  295. return false;
  296. } else {
  297. var n = t - this.startTime;
  298. this.state = n / this.options.duration;
  299. // Perform the easing function, defaults to swing
  300. this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration);
  301. this.now = this.start + ((this.end - this.start) * this.pos);
  302. // Perform the next step of the animation
  303. this.update();
  304. }
  305. return true;
  306. }
  307. };
  308. jQuery.extend( jQuery.fx, {
  309. speeds:{
  310. slow: 600,
  311. fast: 200,
  312. // Default speed
  313. _default: 400
  314. },
  315. step: {
  316. opacity: function(fx){
  317. jQuery.attr(fx.elem.style, "opacity", fx.now);
  318. },
  319. _default: function(fx){
  320. if( fx.prop in fx.elem )
  321. fx.elem[ fx.prop ] = fx.now;
  322. else if( fx.elem.style )
  323. fx.elem.style[ fx.prop ] = fx.now + fx.unit;
  324. }
  325. }
  326. });