Advertisement
killrawr

jquery.resize jQuery 1.8.2 compatible original source

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