10 if ( typeof define ===
'function' && define.amd ) {
12 define([
'jquery'], factory);
13 }
else if (typeof exports ===
'object') {
15 module.exports = factory;
22 var toFix = [
'wheel',
'mousewheel',
'DOMMouseScroll',
'MozMousePixelScroll'],
23 toBind = (
'onwheel' in document || document.documentMode >= 9 ) ?
24 [
'wheel'] : [
'mousewheel',
'DomMouseScroll',
'MozMousePixelScroll'],
25 slice = Array.prototype.slice,
26 nullLowestDeltaTimeout, lowestDelta;
28 if ( $.event.fixHooks ) {
29 for ( var i = toFix.length; i; ) {
30 $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
34 var special = $.event.special.mousewheel = {
38 if ( this.addEventListener ) {
39 for ( var i = toBind.length; i; ) {
40 this.addEventListener( toBind[--i], handler,
false );
43 this.onmousewheel = handler;
46 $.data(
this,
'mousewheel-line-height', special.getLineHeight(
this));
47 $.data(
this,
'mousewheel-page-height', special.getPageHeight(
this));
50 teardown:
function() {
51 if ( this.removeEventListener ) {
52 for ( var i = toBind.length; i; ) {
53 this.removeEventListener( toBind[--i], handler,
false );
56 this.onmousewheel = null;
59 $.removeData(
this,
'mousewheel-line-height');
60 $.removeData(
this,
'mousewheel-page-height');
63 getLineHeight:
function(elem) {
65 $parent = $elem[
'offsetParent' in $.fn ?
'offsetParent' :
'parent']();
66 if (!$parent.length) {
69 return parseInt($parent.css(
'fontSize'), 10) || parseInt($elem.css(
'fontSize'), 10) || 16;
72 getPageHeight:
function(elem) {
73 return $(elem).height();
77 adjustOldDeltas:
true,
83 mousewheel:
function(fn) {
84 return fn ? this.bind(
'mousewheel', fn) : this.trigger(
'mousewheel');
87 unmousewheel:
function(fn) {
88 return this.unbind(
'mousewheel', fn);
93 function handler(event) {
94 var orgEvent =
event || window.event,
95 args = slice.call(arguments, 1),
102 event = $.event.fix(orgEvent);
103 event.type =
'mousewheel';
106 if (
'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; }
107 if (
'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; }
108 if (
'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; }
109 if (
'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; }
112 if (
'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
113 deltaX = deltaY * -1;
118 delta = deltaY === 0 ? deltaX : deltaY;
121 if (
'deltaY' in orgEvent ) {
122 deltaY = orgEvent.deltaY * -1;
125 if (
'deltaX' in orgEvent ) {
126 deltaX = orgEvent.deltaX;
127 if ( deltaY === 0 ) { delta = deltaX * -1; }
131 if ( deltaY === 0 && deltaX === 0 ) {
return; }
138 if ( orgEvent.deltaMode === 1 ) {
139 var lineHeight = $.data(
this,
'mousewheel-line-height');
141 deltaY *= lineHeight;
142 deltaX *= lineHeight;
143 }
else if ( orgEvent.deltaMode === 2 ) {
144 var pageHeight = $.data(
this,
'mousewheel-page-height');
146 deltaY *= pageHeight;
147 deltaX *= pageHeight;
151 absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) );
153 if ( !lowestDelta || absDelta < lowestDelta ) {
154 lowestDelta = absDelta;
157 if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
163 if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
171 delta = Math[ delta >= 1 ?
'floor' :
'ceil' ](delta / lowestDelta);
172 deltaX = Math[ deltaX >= 1 ?
'floor' :
'ceil' ](deltaX / lowestDelta);
173 deltaY = Math[ deltaY >= 1 ?
'floor' :
'ceil' ](deltaY / lowestDelta);
176 if ( special.settings.normalizeOffset &&
this.getBoundingClientRect ) {
177 var boundingRect = this.getBoundingClientRect();
178 offsetX =
event.clientX - boundingRect.left;
179 offsetY =
event.clientY - boundingRect.top;
183 event.deltaX = deltaX;
184 event.deltaY = deltaY;
185 event.deltaFactor = lowestDelta;
186 event.offsetX = offsetX;
187 event.offsetY = offsetY;
194 args.unshift(event, delta, deltaX, deltaY);
200 if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); }
201 nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200);
203 return ($.event.dispatch || $.event.handle).apply(
this, args);
206 function nullLowestDelta() {
210 function shouldAdjustOldDeltas(orgEvent, absDelta) {
218 return special.settings.adjustOldDeltas && orgEvent.type ===
'mousewheel' && absDelta % 120 === 0;