jquery.mousewheel.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Source: https://github.com/jquery/jquery-mousewheel/blob/a06ef4e1a127795606642c55e22d4f2945edc061/jquery.mousewheel.js
  2. /*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
  3. * Licensed under the MIT License (LICENSE.txt).
  4. *
  5. * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
  6. * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
  7. * Thanks to: Seamus Leahy for adding deltaX and deltaY
  8. *
  9. * Version: 3.0.6
  10. *
  11. * Requires: 1.2.2+
  12. */
  13. (function($) {
  14. var types = ['DOMMouseScroll', 'mousewheel'];
  15. if ($.event.fixHooks) {
  16. for ( var i=types.length; i; ) {
  17. $.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
  18. }
  19. }
  20. $.event.special.mousewheel = {
  21. setup: function() {
  22. if ( this.addEventListener ) {
  23. for ( var i=types.length; i; ) {
  24. this.addEventListener( types[--i], handler, false );
  25. }
  26. } else {
  27. this.onmousewheel = handler;
  28. }
  29. },
  30. teardown: function() {
  31. if ( this.removeEventListener ) {
  32. for ( var i=types.length; i; ) {
  33. this.removeEventListener( types[--i], handler, false );
  34. }
  35. } else {
  36. this.onmousewheel = null;
  37. }
  38. }
  39. };
  40. $.fn.extend({
  41. mousewheel: function(fn) {
  42. return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
  43. },
  44. unmousewheel: function(fn) {
  45. return this.unbind("mousewheel", fn);
  46. }
  47. });
  48. function handler(event) {
  49. var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
  50. event = $.event.fix(orgEvent);
  51. event.type = "mousewheel";
  52. // Old school scrollwheel delta
  53. if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
  54. if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }
  55. // New school multidimensional scroll (touchpads) deltas
  56. deltaY = delta;
  57. // Gecko
  58. if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
  59. deltaY = 0;
  60. deltaX = -1*delta;
  61. }
  62. // Webkit
  63. if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
  64. if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
  65. // Add event and delta to the front of the arguments
  66. args.unshift(event, delta, deltaX, deltaY);
  67. return ($.event.dispatch || $.event.handle).apply(this, args);
  68. }
  69. })(jQuery);