jquery.resize.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * windowResize
  3. * Attach it to the window to delay it, catch if resize is more important than XXpx or simply to catch the good IE resize event (IE < 9)
  4. *
  5. * Latest version and complete README available on Github:
  6. * https://github.com/intraordinaire/window_resize
  7. *
  8. * @version 1.1.1
  9. * Copyright 2012 Vincent Garcia
  10. * @author Vincent Garcia https://github.com/intraordinaire/
  11. * Licensed under the MIT license.
  12. */
  13. (function ($) {
  14. $.windowResize = function (element, options) {
  15. this.settings = $.extend(true, {}, $.windowResize.defaults, options);
  16. this.element = element;
  17. this.init();
  18. };
  19. $.extend($.windowResize,
  20. {
  21. window_height: 0, //Save window height to check if really resized
  22. window_width: 0, //Save window width to check if really resized
  23. timer: 0, //Timer to delay the event
  24. evt: null, //The event who is catch
  25. defaults: {
  26. timeout: 0, //The delay, in ms
  27. callback: null, //The callback fn
  28. diff_height: false, //The min diff height, Integer, before firing the event
  29. step_height: false, //A table with heights, Integer, for those the event has to be fired
  30. diff_width: false, //The min diff width, Integer, before firing the event
  31. step_width: false, //A table with widths, Integer, for those the event has to be fired
  32. type: 'both' //If we look at height, width or both resize
  33. },
  34. prototype: {
  35. init: function () {
  36. if (this.settings.callback == null) {
  37. return;
  38. }
  39. if (this.settings.type != 'both' && this.settings.type != 'width' && this.settings.type != 'height') {
  40. this.settings.type = 'both';
  41. }
  42. var $window = $(window);
  43. this.window_height = $window.height();
  44. this.window_width = $window.width();
  45. $window.resize($.proxy(this, 'handleResize'));
  46. },
  47. /**
  48. * Save event fired by the browser to be able to throw it if it's ok
  49. * @param evt
  50. */
  51. handleResize: function (evt) {
  52. this.evt = evt;
  53. if (this.isResized()) { //If event is ok
  54. //Check if we have a timeout or not
  55. if (this.settings.timeout != false && !isNaN(this.settings.timeout)) { //Delay event
  56. this.delayEvent();
  57. } else { //Send event
  58. this.sendEvent();
  59. }
  60. }
  61. },
  62. /**
  63. * Check if resize is effective and complies with the parameters
  64. * @return {boolean} true if it's a real resize with, who complies with params, false otherwise
  65. */
  66. isResized: function () {
  67. //If it's IE or if we have to respect some parameters
  68. if (($.browser.msie && parseInt($.browser.version, 10) < 9) || (this.settings.diff_height != false || this.settings.diff_width != false) || (this.settings.step_height != false || this.settings.step_width != false) || this.settings.type != 'both') {
  69. //Get the current window height, and check height param
  70. var current_height = $(window).height();
  71. var checked_height = this.checkSize(current_height, this.settings.diff_height, this.settings.step_height, this.window_height);
  72. //Get the current window width, and check width params
  73. var current_width = $(window).width();
  74. var checked_width = this.checkSize(current_width, this.settings.diff_width, this.settings.step_width, this.window_width);
  75. //If width or height changed, resize is valid
  76. if ((this.settings.type == 'both' && (checked_height || checked_width)) || (this.settings.type == 'height' && checked_height) || (this.settings.type == 'width' && checked_width)) {
  77. this.window_height = current_height;
  78. this.window_width = current_width;
  79. return true;
  80. }
  81. return false;
  82. }
  83. return true;
  84. },
  85. /**
  86. * Send event
  87. */
  88. sendEvent: function () {
  89. this.settings.callback.apply(this, [this.evt]);
  90. },
  91. /**
  92. * For a given size (width or height) check the paramters
  93. * @param current_size current widht or height
  94. * @param diff_size min diff size
  95. * @param step_size step table
  96. * @param window_size saved window size
  97. * @return {boolean} true if the current size is compliant with the others params, false otherwise
  98. */
  99. checkSize: function (current_size, diff_size, step_size, window_size) {
  100. return (!diff_size && !step_size && current_size != window_size)
  101. ||
  102. (
  103. diff_size
  104. &&
  105. (
  106. parseInt(diff_size) < parseInt(window_size - current_size)
  107. ||
  108. (parseInt(diff_size) * -1) > parseInt(window_size - current_size)
  109. )
  110. )
  111. ||
  112. (
  113. typeof step_size == 'object'
  114. &&
  115. this.inStep(current_size, window_size, step_size)
  116. );
  117. },
  118. /**
  119. * Delay the "sendEvent" function with de timeout
  120. */
  121. delayEvent: function () {
  122. clearTimeout(this.timer);
  123. this.timer = setTimeout($.proxy(this, 'sendEvent'), parseInt(this.settings.timeout));
  124. },
  125. /**
  126. * Check if the current size is between the saved one, and one of the given steps
  127. * @param current The current size
  128. * @param saved The saved window size
  129. * @param step The steps
  130. * @return {boolean} true if size is between, false otherwise
  131. */
  132. inStep: function (current, saved, step) {
  133. var length = step.length;
  134. for (var i = 0; i < length; i++) {
  135. if (step[i] == current || step[i] == saved || (step[i] < current && step[i] > saved) || (step[i] > current && step[i] < saved)) return true;
  136. }
  137. return false;
  138. }
  139. }
  140. });
  141. $.fn.windowResize = function (options) {
  142. return this.each(function () {
  143. if (undefined == $(this).data('windowResize')) {
  144. if (this == window) {
  145. $(this).data('windowResize', new $.windowResize(this, options));
  146. }
  147. else if (!undefined(options.callback)) {
  148. $(this).resize(options.callback);
  149. }
  150. }
  151. });
  152. }
  153. })(jQuery);