PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/src/fx.js

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