PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/jquery/src/fx.js

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