25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
8.1KB

  1. /*!
  2. * jQuery Mousewheel 3.1.13
  3. *
  4. * Copyright jQuery Foundation and other contributors
  5. * Released under the MIT license
  6. * http://jquery.org/license
  7. */
  8. (function (factory) {
  9. if ( typeof define === 'function' && define.amd ) {
  10. // AMD. Register as an anonymous module.
  11. define(['jquery'], factory);
  12. } else if (typeof exports === 'object') {
  13. // Node/CommonJS style for Browserify
  14. module.exports = factory;
  15. } else {
  16. // Browser globals
  17. factory(jQuery);
  18. }
  19. }(function ($) {
  20. var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'],
  21. toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ?
  22. ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'],
  23. slice = Array.prototype.slice,
  24. nullLowestDeltaTimeout, lowestDelta;
  25. if ( $.event.fixHooks ) {
  26. for ( var i = toFix.length; i; ) {
  27. $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
  28. }
  29. }
  30. var special = $.event.special.mousewheel = {
  31. version: '3.1.12',
  32. setup: function() {
  33. if ( this.addEventListener ) {
  34. for ( var i = toBind.length; i; ) {
  35. this.addEventListener( toBind[--i], handler, false );
  36. }
  37. } else {
  38. this.onmousewheel = handler;
  39. }
  40. // Store the line height and page height for this particular element
  41. $.data(this, 'mousewheel-line-height', special.getLineHeight(this));
  42. $.data(this, 'mousewheel-page-height', special.getPageHeight(this));
  43. },
  44. teardown: function() {
  45. if ( this.removeEventListener ) {
  46. for ( var i = toBind.length; i; ) {
  47. this.removeEventListener( toBind[--i], handler, false );
  48. }
  49. } else {
  50. this.onmousewheel = null;
  51. }
  52. // Clean up the data we added to the element
  53. $.removeData(this, 'mousewheel-line-height');
  54. $.removeData(this, 'mousewheel-page-height');
  55. },
  56. getLineHeight: function(elem) {
  57. var $elem = $(elem),
  58. $parent = $elem['offsetParent' in $.fn ? 'offsetParent' : 'parent']();
  59. if (!$parent.length) {
  60. $parent = $('body');
  61. }
  62. return parseInt($parent.css('fontSize'), 10) || parseInt($elem.css('fontSize'), 10) || 16;
  63. },
  64. getPageHeight: function(elem) {
  65. return $(elem).height();
  66. },
  67. settings: {
  68. adjustOldDeltas: true, // see shouldAdjustOldDeltas() below
  69. normalizeOffset: true // calls getBoundingClientRect for each event
  70. }
  71. };
  72. $.fn.extend({
  73. mousewheel: function(fn) {
  74. return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel');
  75. },
  76. unmousewheel: function(fn) {
  77. return this.unbind('mousewheel', fn);
  78. }
  79. });
  80. function handler(event) {
  81. var orgEvent = event || window.event,
  82. args = slice.call(arguments, 1),
  83. delta = 0,
  84. deltaX = 0,
  85. deltaY = 0,
  86. absDelta = 0,
  87. offsetX = 0,
  88. offsetY = 0;
  89. event = $.event.fix(orgEvent);
  90. event.type = 'mousewheel';
  91. // Old school scrollwheel delta
  92. if ( 'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; }
  93. if ( 'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; }
  94. if ( 'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; }
  95. if ( 'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; }
  96. // Firefox < 17 horizontal scrolling related to DOMMouseScroll event
  97. if ( 'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
  98. deltaX = deltaY * -1;
  99. deltaY = 0;
  100. }
  101. // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy
  102. delta = deltaY === 0 ? deltaX : deltaY;
  103. // New school wheel delta (wheel event)
  104. if ( 'deltaY' in orgEvent ) {
  105. deltaY = orgEvent.deltaY * -1;
  106. delta = deltaY;
  107. }
  108. if ( 'deltaX' in orgEvent ) {
  109. deltaX = orgEvent.deltaX;
  110. if ( deltaY === 0 ) { delta = deltaX * -1; }
  111. }
  112. // No change actually happened, no reason to go any further
  113. if ( deltaY === 0 && deltaX === 0 ) { return; }
  114. // Need to convert lines and pages to pixels if we aren't already in pixels
  115. // There are three delta modes:
  116. // * deltaMode 0 is by pixels, nothing to do
  117. // * deltaMode 1 is by lines
  118. // * deltaMode 2 is by pages
  119. if ( orgEvent.deltaMode === 1 ) {
  120. var lineHeight = $.data(this, 'mousewheel-line-height');
  121. delta *= lineHeight;
  122. deltaY *= lineHeight;
  123. deltaX *= lineHeight;
  124. } else if ( orgEvent.deltaMode === 2 ) {
  125. var pageHeight = $.data(this, 'mousewheel-page-height');
  126. delta *= pageHeight;
  127. deltaY *= pageHeight;
  128. deltaX *= pageHeight;
  129. }
  130. // Store lowest absolute delta to normalize the delta values
  131. absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) );
  132. if ( !lowestDelta || absDelta < lowestDelta ) {
  133. lowestDelta = absDelta;
  134. // Adjust older deltas if necessary
  135. if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
  136. lowestDelta /= 40;
  137. }
  138. }
  139. // Adjust older deltas if necessary
  140. if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
  141. // Divide all the things by 40!
  142. delta /= 40;
  143. deltaX /= 40;
  144. deltaY /= 40;
  145. }
  146. // Get a whole, normalized value for the deltas
  147. delta = Math[ delta >= 1 ? 'floor' : 'ceil' ](delta / lowestDelta);
  148. deltaX = Math[ deltaX >= 1 ? 'floor' : 'ceil' ](deltaX / lowestDelta);
  149. deltaY = Math[ deltaY >= 1 ? 'floor' : 'ceil' ](deltaY / lowestDelta);
  150. // Normalise offsetX and offsetY properties
  151. if ( special.settings.normalizeOffset && this.getBoundingClientRect ) {
  152. var boundingRect = this.getBoundingClientRect();
  153. offsetX = event.clientX - boundingRect.left;
  154. offsetY = event.clientY - boundingRect.top;
  155. }
  156. // Add information to the event object
  157. event.deltaX = deltaX;
  158. event.deltaY = deltaY;
  159. event.deltaFactor = lowestDelta;
  160. event.offsetX = offsetX;
  161. event.offsetY = offsetY;
  162. // Go ahead and set deltaMode to 0 since we converted to pixels
  163. // Although this is a little odd since we overwrite the deltaX/Y
  164. // properties with normalized deltas.
  165. event.deltaMode = 0;
  166. // Add event and delta to the front of the arguments
  167. args.unshift(event, delta, deltaX, deltaY);
  168. // Clearout lowestDelta after sometime to better
  169. // handle multiple device types that give different
  170. // a different lowestDelta
  171. // Ex: trackpad = 3 and mouse wheel = 120
  172. if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); }
  173. nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200);
  174. return ($.event.dispatch || $.event.handle).apply(this, args);
  175. }
  176. function nullLowestDelta() {
  177. lowestDelta = null;
  178. }
  179. function shouldAdjustOldDeltas(orgEvent, absDelta) {
  180. // If this is an older event and the delta is divisable by 120,
  181. // then we are assuming that the browser is treating this as an
  182. // older mouse wheel event and that we should divide the deltas
  183. // by 40 to try and get a more usable deltaFactor.
  184. // Side note, this actually impacts the reported scroll distance
  185. // in older browsers and can cause scrolling to be slower than native.
  186. // Turn this off by setting $.event.special.mousewheel.settings.adjustOldDeltas to false.
  187. return special.settings.adjustOldDeltas && orgEvent.type === 'mousewheel' && absDelta % 120 === 0;
  188. }
  189. }));