Skip to content

Commit 2e2dc57

Browse files
committed
Use new wheel event if available (fixes Firefox 17+ issues and adds horizontal support to IE9+)
Normalize the detal values Support AMD loaders Bump version to 3.1.0 and update copyright
1 parent 7b08e4b commit 2e2dc57

File tree

4 files changed

+124
-70
lines changed

4 files changed

+124
-70
lines changed

ChangeLog.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# Mouse Wheel ChangeLog
22

3+
# 3.1.0
4+
5+
* Fix Firefox 17+ issues by using new wheel event
6+
* Normalize delta values
7+
* Adds horizontal support for IE 9+ by using new wheel event
8+
* Support AMD loaders
9+
10+
311
# 3.0.6
412

513
* Fix issue with delta being 0 in Firefox
614

15+
716
# 3.0.5
817

918
* jQuery 1.7 compatibility
1019

20+
1121
# 3.0.4
1222

1323
* Fix IE issue

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
A jQuery plugin that adds cross-browser mouse wheel support.
44

5-
In order to use the plugin, simply bind the "mousewheel" event to an element. It also provides two helper methods called `mousewheel` and `unmousewheel` that act just like other event helper methods in jQuery. The event callback receives three extra arguments which are the normalized "deltas" of the mouse wheel.
5+
In order to use the plugin, simply bind the "mousewheel" event to an element. It also provides two helper methods called `mousewheel` and `unmousewheel` that act just like other event helper methods in jQuery. The event callback receives three extra arguments which are the normalized "deltas" of the mouse wheel.
66

77
Here is an example of using both the bind and helper method syntax.
88

99
// using bind
1010
$('#my_elem').bind('mousewheel', function(event, delta, deltaX, deltaY) {
1111
console.log(delta, deltaX, deltaY);
1212
});
13-
13+
1414
// using the event helper
1515
$('#my_elem').mousewheel(function(event, delta, deltaX, deltaY) {
1616
console.log(delta, deltaX, deltaY);
@@ -21,4 +21,4 @@ Here is an example of using both the bind and helper method syntax.
2121

2222
This plugin is licensed under the MIT License (LICENSE.txt).
2323

24-
Copyright (c) 2011 [Brandon Aaron](http://brandonaaron.net)
24+
Copyright (c) 2013 [Brandon Aaron](http://brandonaaron.net)

jquery.mousewheel.js

Lines changed: 84 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,101 @@
1-
/*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
1+
/*! Copyright (c) 2013 Brandon Aaron (http://brandonaaron.net)
22
* Licensed under the MIT License (LICENSE.txt).
33
*
44
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
55
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
66
* Thanks to: Seamus Leahy for adding deltaX and deltaY
77
*
8-
* Version: 3.0.6
9-
*
8+
* Version: 3.1.0
9+
*
1010
* Requires: 1.2.2+
1111
*/
1212

13-
(function($) {
13+
(function (factory) {
14+
if (typeof define === 'function' && define.amd) {
15+
// AMD. Register as an anonymous module.
16+
define(['jquery'], factory);
17+
} else {
18+
// Browser globals
19+
factory(jQuery);
20+
}
21+
}(function ($) {
1422

15-
var types = ['DOMMouseScroll', 'mousewheel'];
23+
var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll'];
24+
var toBind = 'onwheel' in document || document.documentMode >= 9 ? ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'];
25+
var lowestDelta, lowestDeltaXY;
1626

17-
if ($.event.fixHooks) {
18-
for ( var i=types.length; i; ) {
19-
$.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
27+
if ($.event.fixHooks) {
28+
for ( var i=toFix.length; i; ) {
29+
$.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
30+
}
2031
}
21-
}
2232

23-
$.event.special.mousewheel = {
24-
setup: function() {
25-
if ( this.addEventListener ) {
26-
for ( var i=types.length; i; ) {
27-
this.addEventListener( types[--i], handler, false );
33+
$.event.special.mousewheel = {
34+
setup: function() {
35+
if ( this.addEventListener ) {
36+
for ( var i=toBind.length; i; ) {
37+
this.addEventListener( toBind[--i], handler, false );
38+
}
39+
} else {
40+
this.onmousewheel = handler;
2841
}
29-
} else {
30-
this.onmousewheel = handler;
31-
}
32-
},
33-
34-
teardown: function() {
35-
if ( this.removeEventListener ) {
36-
for ( var i=types.length; i; ) {
37-
this.removeEventListener( types[--i], handler, false );
42+
},
43+
44+
teardown: function() {
45+
if ( this.removeEventListener ) {
46+
for ( var i=toBind.length; i; ) {
47+
this.removeEventListener( toBind[--i], handler, false );
48+
}
49+
} else {
50+
this.onmousewheel = null;
3851
}
39-
} else {
40-
this.onmousewheel = null;
4152
}
53+
};
54+
55+
$.fn.extend({
56+
mousewheel: function(fn) {
57+
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
58+
},
59+
60+
unmousewheel: function(fn) {
61+
return this.unbind("mousewheel", fn);
62+
}
63+
});
64+
65+
66+
function handler(event) {
67+
var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, deltaX = 0, deltaY = 0, absDelta = 0, absDeltaXY = 0;
68+
event = $.event.fix(orgEvent);
69+
event.type = "mousewheel";
70+
71+
// Old school scrollwheel delta
72+
if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta; }
73+
if ( orgEvent.detail ) { delta = orgEvent.detail * -1; }
74+
75+
// New school wheel delta (wheel event)
76+
if ( orgEvent.deltaY ) {
77+
deltaY = orgEvent.deltaY * -1;
78+
delta = deltaY;
79+
}
80+
if ( orgEvent.deltaX ) {
81+
deltaX = orgEvent.deltaX;
82+
delta = deltaX * -1;
83+
}
84+
85+
// Webkit
86+
if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY; }
87+
if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = orgEvent.wheelDeltaX * -1; }
88+
89+
absDelta = Math.abs(delta);
90+
if ( !lowestDelta || absDelta < lowestDelta ) { lowestDelta = absDelta; }
91+
92+
absDeltaXY = Math.max( Math.abs(deltaY), Math.abs(deltaX) );
93+
if ( !lowestDeltaXY || absDeltaXY < lowestDeltaXY ) { lowestDeltaXY = absDeltaXY; }
94+
95+
// Add event and delta to the front of the arguments
96+
args.unshift(event, Math.floor(delta/lowestDelta), Math.floor(deltaX/lowestDeltaXY), Math.floor(deltaY/lowestDeltaXY));
97+
98+
return ($.event.dispatch || $.event.handle).apply(this, args);
4299
}
43-
};
44-
45-
$.fn.extend({
46-
mousewheel: function(fn) {
47-
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
48-
},
49-
50-
unmousewheel: function(fn) {
51-
return this.unbind("mousewheel", fn);
52-
}
53-
});
54-
55-
56-
function handler(event) {
57-
var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
58-
event = $.event.fix(orgEvent);
59-
event.type = "mousewheel";
60-
61-
// Old school scrollwheel delta
62-
if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
63-
if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }
64-
65-
// New school multidimensional scroll (touchpads) deltas
66-
deltaY = delta;
67-
68-
// Gecko
69-
if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
70-
deltaY = 0;
71-
deltaX = -1*delta;
72-
}
73-
74-
// Webkit
75-
if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
76-
if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
77-
78-
// Add event and delta to the front of the arguments
79-
args.unshift(event, delta, deltaX, deltaY);
80-
81-
return ($.event.dispatch || $.event.handle).apply(this, args);
82-
}
83-
84-
})(jQuery);
100+
101+
}));

mousewheel.jquery.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "mousewheel",
3+
"title": "jQuery Mousewheel",
4+
"description": "A jQuery plugin that adds cross-browser mouse wheel support.",
5+
"keywords": [
6+
"mousewheel",
7+
"mouse",
8+
"event"
9+
],
10+
"version": "3.1.0",
11+
"author": {
12+
"name": "Brandon Aaron",
13+
"url": "http://brandonaaron.net"
14+
},
15+
"licenses": [
16+
{
17+
"type": "MIT",
18+
"url": "https://raw.github.com/brandonaaron/jquery-mousewheel/master/LICENSE.txt"
19+
}
20+
],
21+
"bugs": "https://github.com/brandonaaron/jquery-mousewheel/issues",
22+
"homepage": "https://github.com/brandonaaron/jquery-mousewheel",
23+
"download": "https://github.com/brandonaaron/jquery-mousewheel/tags",
24+
"dependencies": {
25+
"jquery": ">=1.2.2"
26+
}
27+
}

0 commit comments

Comments
 (0)