jquery.mousewheel.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*! Copyright (c) 2011 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.0.6
  9. *
  10. * Requires: 1.2.2+
  11. */
  12. (function($) {
  13. var types = ['DOMMouseScroll', 'mousewheel'];
  14. if ($.event.fixHooks) {
  15. for ( var i=types.length; i; ) {
  16. $.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
  17. }
  18. }
  19. $.event.special.mousewheel = {
  20. setup: function() {
  21. if ( this.addEventListener ) {
  22. for ( var i=types.length; i; ) {
  23. this.addEventListener( types[--i], handler, false );
  24. }
  25. } else {
  26. this.onmousewheel = handler;
  27. }
  28. },
  29. teardown: function() {
  30. if ( this.removeEventListener ) {
  31. for ( var i=types.length; i; ) {
  32. this.removeEventListener( types[--i], handler, false );
  33. }
  34. } else {
  35. this.onmousewheel = null;
  36. }
  37. }
  38. };
  39. $.fn.extend({
  40. mousewheel: function(fn) {
  41. return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
  42. },
  43. unmousewheel: function(fn) {
  44. return this.unbind("mousewheel", fn);
  45. }
  46. });
  47. function handler(event) {
  48. var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
  49. event = $.event.fix(orgEvent);
  50. event.type = "mousewheel";
  51. // Old school scrollwheel delta
  52. if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
  53. if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }
  54. // New school multidimensional scroll (touchpads) deltas
  55. deltaY = delta;
  56. // Gecko
  57. if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
  58. deltaY = 0;
  59. deltaX = -1*delta;
  60. }
  61. // Webkit
  62. if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
  63. if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
  64. // Add event and delta to the front of the arguments
  65. args.unshift(event, delta, deltaX, deltaY);
  66. return ($.event.dispatch || $.event.handle).apply(this, args);
  67. }
  68. })(jQuery);