jquery.mousewheel.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*! Copyright (c) 2013 Brandon Aaron (http://brandonaaron.net)
  2. * Licensed under the MIT License (LICENSE.txt).
  3. *
  4. * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
  5. * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
  6. * Thanks to: Seamus Leahy for adding deltaX and deltaY
  7. *
  8. * Version: 3.1.3
  9. *
  10. * Requires: 1.2.2+
  11. */
  12. (function (factory) {
  13. if ( typeof define === 'function' && define.amd ) {
  14. // AMD. Register as an anonymous module.
  15. define(['jquery'], factory);
  16. } else if (typeof exports === 'object') {
  17. // Node/CommonJS style for Browserify
  18. module.exports = factory;
  19. } else {
  20. // Browser globals
  21. factory(jQuery);
  22. }
  23. }(function ($) {
  24. var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'];
  25. var toBind = 'onwheel' in document || document.documentMode >= 9 ? ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'];
  26. var lowestDelta, lowestDeltaXY;
  27. if ( $.event.fixHooks ) {
  28. for ( var i = toFix.length; i; ) {
  29. $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
  30. }
  31. }
  32. $.event.special.mousewheel = {
  33. setup: function() {
  34. if ( this.addEventListener ) {
  35. for ( var i = toBind.length; i; ) {
  36. this.addEventListener( toBind[--i], handler, false );
  37. }
  38. } else {
  39. this.onmousewheel = handler;
  40. }
  41. },
  42. teardown: function() {
  43. if ( this.removeEventListener ) {
  44. for ( var i = toBind.length; i; ) {
  45. this.removeEventListener( toBind[--i], handler, false );
  46. }
  47. } else {
  48. this.onmousewheel = null;
  49. }
  50. }
  51. };
  52. $.fn.extend({
  53. mousewheel: function(fn) {
  54. return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
  55. },
  56. unmousewheel: function(fn) {
  57. return this.unbind("mousewheel", fn);
  58. }
  59. });
  60. function handler(event) {
  61. var orgEvent = event || window.event,
  62. args = [].slice.call(arguments, 1),
  63. delta = 0,
  64. deltaX = 0,
  65. deltaY = 0,
  66. absDelta = 0,
  67. absDeltaXY = 0,
  68. fn;
  69. event = $.event.fix(orgEvent);
  70. event.type = "mousewheel";
  71. // Old school scrollwheel delta
  72. if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta; }
  73. if ( orgEvent.detail ) { delta = orgEvent.detail * -1; }
  74. // New school wheel delta (wheel event)
  75. if ( orgEvent.deltaY ) {
  76. deltaY = orgEvent.deltaY * -1;
  77. delta = deltaY;
  78. }
  79. if ( orgEvent.deltaX ) {
  80. deltaX = orgEvent.deltaX;
  81. delta = deltaX * -1;
  82. }
  83. // Webkit
  84. if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY; }
  85. if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = orgEvent.wheelDeltaX * -1; }
  86. // Look for lowest delta to normalize the delta values
  87. absDelta = Math.abs(delta);
  88. if ( !lowestDelta || absDelta < lowestDelta ) { lowestDelta = absDelta; }
  89. absDeltaXY = Math.max(Math.abs(deltaY), Math.abs(deltaX));
  90. if ( !lowestDeltaXY || absDeltaXY < lowestDeltaXY ) { lowestDeltaXY = absDeltaXY; }
  91. // Get a whole value for the deltas
  92. fn = delta > 0 ? 'floor' : 'ceil';
  93. delta = Math[fn](delta / lowestDelta);
  94. deltaX = Math[fn](deltaX / lowestDeltaXY);
  95. deltaY = Math[fn](deltaY / lowestDeltaXY);
  96. // Add event and delta to the front of the arguments
  97. args.unshift(event, delta, deltaX, deltaY);
  98. return ($.event.dispatch || $.event.handle).apply(this, args);
  99. }
  100. }));