1 /*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
2 * Licensed under the MIT License (LICENSE.txt).
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
15 var types = ['DOMMouseScroll', 'mousewheel'];
17 $.event.special.mousewheel = {
19 if ( this.addEventListener ) {
20 for ( var i=types.length; i; ) {
21 this.addEventListener( types[--i], handler, false );
24 this.onmousewheel = handler;
28 teardown: function() {
29 if ( this.removeEventListener ) {
30 for ( var i=types.length; i; ) {
31 this.removeEventListener( types[--i], handler, false );
34 this.onmousewheel = null;
40 mousewheel: function(fn) {
41 return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
44 unmousewheel: function(fn) {
45 return this.unbind("mousewheel", fn);
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";
55 // Old school scrollwheel delta
56 if ( event.wheelDelta ) { delta = event.wheelDelta/120; }
57 if ( event.detail ) { delta = -event.detail/3; }
59 // New school multidimensional scroll (touchpads) deltas
63 if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
69 if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
70 if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
72 // Add event and delta to the front of the arguments
73 args.unshift(event, delta, deltaX, deltaY);
75 return $.event.handle.apply(this, args);