PageRenderTime 51ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/jquery-resize/1.0/jquery.ba-resize.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 238 lines | 63 code | 36 blank | 139 comment | 12 complexity | 7d3c2c913fa32060c8a38240469a86ac MD5 | raw file
  1. /*!
  2. * jQuery resize event - v1.0 - 2/10/2010
  3. * http://benalman.com/projects/jquery-resize-plugin/
  4. *
  5. * Copyright (c) 2010 "Cowboy" Ben Alman
  6. * Dual licensed under the MIT and GPL licenses.
  7. * http://benalman.com/about/license/
  8. */
  9. // Script: jQuery resize event
  10. //
  11. // *Version: 1.0, Last updated: 2/10/2010*
  12. //
  13. // Project Home - http://benalman.com/projects/jquery-resize-plugin/
  14. // GitHub - http://github.com/cowboy/jquery-resize/
  15. // Source - http://github.com/cowboy/jquery-resize/raw/master/jquery.ba-resize.js
  16. // (Minified) - http://github.com/cowboy/jquery-resize/raw/master/jquery.ba-resize.min.js (1.0kb)
  17. //
  18. // About: License
  19. //
  20. // Copyright (c) 2010 "Cowboy" Ben Alman,
  21. // Dual licensed under the MIT and GPL licenses.
  22. // http://benalman.com/about/license/
  23. //
  24. // About: Examples
  25. //
  26. // This working example, complete with fully commented code, illustrates a few
  27. // ways in which this plugin can be used.
  28. //
  29. // resize event - http://benalman.com/code/projects/jquery-resize/examples/resize/
  30. //
  31. // About: Support and Testing
  32. //
  33. // Information about what version or versions of jQuery this plugin has been
  34. // tested with, what browsers it has been tested in, and where the unit tests
  35. // reside (so you can test it yourself).
  36. //
  37. // jQuery Versions - 1.3.2, 1.4.1, 1.4.2pre
  38. // Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.
  39. // Unit Tests - http://benalman.com/code/projects/jquery-resize/unit/
  40. //
  41. // About: Release History
  42. //
  43. // 1.0 - (2/10/2010) Initial release
  44. (function($,window,undefined){
  45. '$:nomunge'; // Used by YUI compressor.
  46. // A jQuery object containing all non-window elements to which the resize
  47. // event is bound.
  48. var elems = $([]),
  49. // Extend $.resize if it already exists, otherwise create it.
  50. jq_resize = $.resize = $.extend( $.resize, {} ),
  51. timeout_id,
  52. // Reused strings.
  53. str_setTimeout = 'setTimeout',
  54. str_resize = 'resize',
  55. str_data = str_resize + '-special-event',
  56. str_delay = 'delay',
  57. str_throttle = 'throttleWindow';
  58. // Property: jQuery.resize.delay
  59. //
  60. // The numeric interval (in milliseconds) at which the resize event polling
  61. // loop executes. Defaults to 250.
  62. jq_resize[ str_delay ] = 250;
  63. // Property: jQuery.resize.throttleWindow
  64. //
  65. // Throttle the native window object resize event to fire no more than once
  66. // every <jQuery.resize.delay> milliseconds. Defaults to true.
  67. //
  68. // Because the window object has its own resize event, it doesn't need to be
  69. // provided by this plugin, and its execution can be left entirely up to the
  70. // browser. However, since certain browsers fire the resize event continuously
  71. // while others do not, enabling this will throttle the window resize event,
  72. // making event behavior consistent across all elements in all browsers.
  73. //
  74. // While setting this property to false will disable window object resize
  75. // event throttling, please note that this property must be changed before any
  76. // window object resize event callbacks are bound.
  77. jq_resize[ str_throttle ] = true;
  78. // Event: resize event
  79. //
  80. // Fired when an element's width or height changes. Because browsers only
  81. // provide this event for the window element, for other elements a polling
  82. // loop is initialized, running every <jQuery.resize.delay> milliseconds
  83. // to see if elements' dimensions have changed. You may bind with either
  84. // .resize( fn ) or .bind( "resize", fn ), and unbind with .unbind( "resize" ).
  85. //
  86. // Usage:
  87. //
  88. // > jQuery('selector').bind( 'resize', function(e) {
  89. // > // element's width or height has changed!
  90. // > ...
  91. // > });
  92. //
  93. // Additional Notes:
  94. //
  95. // * The polling loop is not created until at least one callback is actually
  96. // bound to the 'resize' event, and this single polling loop is shared
  97. // across all elements.
  98. //
  99. // Double firing issue in jQuery 1.3.2:
  100. //
  101. // While this plugin works in jQuery 1.3.2, if an element's event callbacks
  102. // are manually triggered via .trigger( 'resize' ) or .resize() those
  103. // callbacks may double-fire, due to limitations in the jQuery 1.3.2 special
  104. // events system. This is not an issue when using jQuery 1.4+.
  105. //
  106. // > // While this works in jQuery 1.4+
  107. // > $(elem).css({ width: new_w, height: new_h }).resize();
  108. // >
  109. // > // In jQuery 1.3.2, you need to do this:
  110. // > var elem = $(elem);
  111. // > elem.css({ width: new_w, height: new_h });
  112. // > elem.data( 'resize-special-event', { width: elem.width(), height: elem.height() } );
  113. // > elem.resize();
  114. $.event.special[ str_resize ] = {
  115. // Called only when the first 'resize' event callback is bound per element.
  116. setup: function() {
  117. // Since window has its own native 'resize' event, return false so that
  118. // jQuery will bind the event using DOM methods. Since only 'window'
  119. // objects have a .setTimeout method, this should be a sufficient test.
  120. // Unless, of course, we're throttling the 'resize' event for window.
  121. if ( !jq_resize[ str_throttle ] && this[ str_setTimeout ] ) { return false; }
  122. var elem = $(this);
  123. // Add this element to the list of internal elements to monitor.
  124. elems = elems.add( elem );
  125. // Initialize data store on the element.
  126. elem.data( str_data, { width: elem.width(), height: elem.height() } );
  127. // If this is the first element added, start the polling loop.
  128. if ( elems.length === 1 ) {
  129. loopy();
  130. }
  131. },
  132. // Called only when the last 'resize' event callback is unbound per element.
  133. teardown: function() {
  134. // Since window has its own native 'resize' event, return false so that
  135. // jQuery will unbind the event using DOM methods. Since only 'window'
  136. // objects have a .setTimeout method, this should be a sufficient test.
  137. // Unless, of course, we're throttling the 'resize' event for window.
  138. if ( !jq_resize[ str_throttle ] && this[ str_setTimeout ] ) { return false; }
  139. var elem = $(this);
  140. // Remove this element from the list of internal elements to monitor.
  141. elems = elems.not( elem );
  142. // Remove any data stored on the element.
  143. elem.removeData( str_data );
  144. // If this is the last element removed, stop the polling loop.
  145. if ( !elems.length ) {
  146. clearTimeout( timeout_id );
  147. }
  148. },
  149. // Called every time a 'resize' event callback is bound per element (new in
  150. // jQuery 1.4).
  151. add: function( handleObj ) {
  152. // Since window has its own native 'resize' event, return false so that
  153. // jQuery doesn't modify the event object. Unless, of course, we're
  154. // throttling the 'resize' event for window.
  155. if ( !jq_resize[ str_throttle ] && this[ str_setTimeout ] ) { return false; }
  156. var old_handler;
  157. // The new_handler function is executed every time the event is triggered.
  158. // This is used to update the internal element data store with the width
  159. // and height when the event is triggered manually, to avoid double-firing
  160. // of the event callback. See the "Double firing issue in jQuery 1.3.2"
  161. // comments above for more information.
  162. function new_handler( e, w, h ) {
  163. var elem = $(this),
  164. data = elem.data( str_data );
  165. // If called from the polling loop, w and h will be passed in as
  166. // arguments. If called manually, via .trigger( 'resize' ) or .resize(),
  167. // those values will need to be computed.
  168. data.w = w !== undefined ? w : elem.width();
  169. data.h = h !== undefined ? h : elem.height();
  170. old_handler.apply( this, arguments );
  171. };
  172. // This may seem a little complicated, but it normalizes the special event
  173. // .add method between jQuery 1.4/1.4.1 and 1.4.2+
  174. if ( $.isFunction( handleObj ) ) {
  175. // 1.4, 1.4.1
  176. old_handler = handleObj;
  177. return new_handler;
  178. } else {
  179. // 1.4.2+
  180. old_handler = handleObj.handler;
  181. handleObj.handler = new_handler;
  182. }
  183. }
  184. };
  185. // Start the polling loop.
  186. function loopy() {
  187. // Iterate over all elements to which the 'resize' event is bound.
  188. elems.each(function(){
  189. var elem = $(this),
  190. width = elem.width(),
  191. height = elem.height(),
  192. data = elem.data( str_data );
  193. // If element size has changed since the last time, update the element
  194. // data store and trigger the 'resize' event.
  195. if ( width !== data.w || height !== data.h ) {
  196. elem.trigger( str_resize, [ data.w = width, data.h = height ] );
  197. }
  198. });
  199. // Loop.
  200. timeout_id = window[ str_setTimeout ]( loopy, jq_resize[ str_delay ] );
  201. };
  202. })(jQuery,this);