PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/fx.js

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