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