jquery.mousewheel-3.0.4.js 2.2 KB

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