Update jquery-mousewheel to 3.1.9
This commit is contained in:
parent
4c0163a2f6
commit
a9000d4b6d
@ -1,84 +1,201 @@
|
|||||||
/*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
|
/*! Copyright (c) 2013 Brandon Aaron (http://brandon.aaron.sh)
|
||||||
* Licensed under the MIT License (LICENSE.txt).
|
* Licensed under the MIT License (LICENSE.txt).
|
||||||
*
|
*
|
||||||
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
|
* Version: 3.1.9
|
||||||
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
|
|
||||||
* Thanks to: Seamus Leahy for adding deltaX and deltaY
|
|
||||||
*
|
*
|
||||||
* Version: 3.0.6
|
* Requires: jQuery 1.2.2+
|
||||||
*
|
|
||||||
* Requires: 1.2.2+
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function($) {
|
(function (factory) {
|
||||||
|
if ( typeof define === 'function' && define.amd ) {
|
||||||
var types = ['DOMMouseScroll', 'mousewheel'];
|
// AMD. Register as an anonymous module.
|
||||||
|
define(['jquery'], factory);
|
||||||
if ($.event.fixHooks) {
|
} else if (typeof exports === 'object') {
|
||||||
for ( var i=types.length; i; ) {
|
// Node/CommonJS style for Browserify
|
||||||
$.event.fixHooks[ types[--i] ] = $.event.mouseHooks;
|
module.exports = factory;
|
||||||
|
} else {
|
||||||
|
// Browser globals
|
||||||
|
factory(jQuery);
|
||||||
}
|
}
|
||||||
}
|
}(function ($) {
|
||||||
|
|
||||||
$.event.special.mousewheel = {
|
var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'],
|
||||||
setup: function() {
|
toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ?
|
||||||
if ( this.addEventListener ) {
|
['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'],
|
||||||
for ( var i=types.length; i; ) {
|
slice = Array.prototype.slice,
|
||||||
this.addEventListener( types[--i], handler, false );
|
nullLowestDeltaTimeout, lowestDelta;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.onmousewheel = handler;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
teardown: function() {
|
if ( $.event.fixHooks ) {
|
||||||
if ( this.removeEventListener ) {
|
for ( var i = toFix.length; i; ) {
|
||||||
for ( var i=types.length; i; ) {
|
$.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
|
||||||
this.removeEventListener( types[--i], handler, false );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.onmousewheel = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
$.fn.extend({
|
var special = $.event.special.mousewheel = {
|
||||||
mousewheel: function(fn) {
|
version: '3.1.9',
|
||||||
return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
|
|
||||||
},
|
|
||||||
|
|
||||||
unmousewheel: function(fn) {
|
setup: function() {
|
||||||
return this.unbind("mousewheel", fn);
|
if ( this.addEventListener ) {
|
||||||
}
|
for ( var i = toBind.length; i; ) {
|
||||||
});
|
this.addEventListener( toBind[--i], handler, false );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.onmousewheel = handler;
|
||||||
|
}
|
||||||
|
// Store the line height and page height for this particular element
|
||||||
|
$.data(this, 'mousewheel-line-height', special.getLineHeight(this));
|
||||||
|
$.data(this, 'mousewheel-page-height', special.getPageHeight(this));
|
||||||
|
},
|
||||||
|
|
||||||
|
teardown: function() {
|
||||||
|
if ( this.removeEventListener ) {
|
||||||
|
for ( var i = toBind.length; i; ) {
|
||||||
|
this.removeEventListener( toBind[--i], handler, false );
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.onmousewheel = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getLineHeight: function(elem) {
|
||||||
|
return parseInt($(elem)['offsetParent' in $.fn ? 'offsetParent' : 'parent']().css('fontSize'), 10);
|
||||||
|
},
|
||||||
|
|
||||||
|
getPageHeight: function(elem) {
|
||||||
|
return $(elem).height();
|
||||||
|
},
|
||||||
|
|
||||||
|
settings: {
|
||||||
|
adjustOldDeltas: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$.fn.extend({
|
||||||
|
mousewheel: function(fn) {
|
||||||
|
return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel');
|
||||||
|
},
|
||||||
|
|
||||||
|
unmousewheel: function(fn) {
|
||||||
|
return this.unbind('mousewheel', fn);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
function handler(event) {
|
function handler(event) {
|
||||||
var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
|
var orgEvent = event || window.event,
|
||||||
event = $.event.fix(orgEvent);
|
args = slice.call(arguments, 1),
|
||||||
event.type = "mousewheel";
|
delta = 0,
|
||||||
|
deltaX = 0,
|
||||||
|
deltaY = 0,
|
||||||
|
absDelta = 0;
|
||||||
|
event = $.event.fix(orgEvent);
|
||||||
|
event.type = 'mousewheel';
|
||||||
|
|
||||||
// Old school scrollwheel delta
|
// Old school scrollwheel delta
|
||||||
if ( orgEvent.wheelDelta ) { delta = orgEvent.wheelDelta/120; }
|
if ( 'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; }
|
||||||
if ( orgEvent.detail ) { delta = -orgEvent.detail/3; }
|
if ( 'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; }
|
||||||
|
if ( 'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; }
|
||||||
|
if ( 'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; }
|
||||||
|
|
||||||
// New school multidimensional scroll (touchpads) deltas
|
// Firefox < 17 horizontal scrolling related to DOMMouseScroll event
|
||||||
deltaY = delta;
|
if ( 'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
|
||||||
|
deltaX = deltaY * -1;
|
||||||
|
deltaY = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Gecko
|
// Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy
|
||||||
if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
|
delta = deltaY === 0 ? deltaX : deltaY;
|
||||||
deltaY = 0;
|
|
||||||
deltaX = -1*delta;
|
// New school wheel delta (wheel event)
|
||||||
|
if ( 'deltaY' in orgEvent ) {
|
||||||
|
deltaY = orgEvent.deltaY * -1;
|
||||||
|
delta = deltaY;
|
||||||
|
}
|
||||||
|
if ( 'deltaX' in orgEvent ) {
|
||||||
|
deltaX = orgEvent.deltaX;
|
||||||
|
if ( deltaY === 0 ) { delta = deltaX * -1; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// No change actually happened, no reason to go any further
|
||||||
|
if ( deltaY === 0 && deltaX === 0 ) { return; }
|
||||||
|
|
||||||
|
// Need to convert lines and pages to pixels if we aren't already in pixels
|
||||||
|
// There are three delta modes:
|
||||||
|
// * deltaMode 0 is by pixels, nothing to do
|
||||||
|
// * deltaMode 1 is by lines
|
||||||
|
// * deltaMode 2 is by pages
|
||||||
|
if ( orgEvent.deltaMode === 1 ) {
|
||||||
|
var lineHeight = $.data(this, 'mousewheel-line-height');
|
||||||
|
delta *= lineHeight;
|
||||||
|
deltaY *= lineHeight;
|
||||||
|
deltaX *= lineHeight;
|
||||||
|
} else if ( orgEvent.deltaMode === 2 ) {
|
||||||
|
var pageHeight = $.data(this, 'mousewheel-page-height');
|
||||||
|
delta *= pageHeight;
|
||||||
|
deltaY *= pageHeight;
|
||||||
|
deltaX *= pageHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store lowest absolute delta to normalize the delta values
|
||||||
|
absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) );
|
||||||
|
|
||||||
|
if ( !lowestDelta || absDelta < lowestDelta ) {
|
||||||
|
lowestDelta = absDelta;
|
||||||
|
|
||||||
|
// Adjust older deltas if necessary
|
||||||
|
if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
|
||||||
|
lowestDelta /= 40;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust older deltas if necessary
|
||||||
|
if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) {
|
||||||
|
// Divide all the things by 40!
|
||||||
|
delta /= 40;
|
||||||
|
deltaX /= 40;
|
||||||
|
deltaY /= 40;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a whole, normalized value for the deltas
|
||||||
|
delta = Math[ delta >= 1 ? 'floor' : 'ceil' ](delta / lowestDelta);
|
||||||
|
deltaX = Math[ deltaX >= 1 ? 'floor' : 'ceil' ](deltaX / lowestDelta);
|
||||||
|
deltaY = Math[ deltaY >= 1 ? 'floor' : 'ceil' ](deltaY / lowestDelta);
|
||||||
|
|
||||||
|
// Add information to the event object
|
||||||
|
event.deltaX = deltaX;
|
||||||
|
event.deltaY = deltaY;
|
||||||
|
event.deltaFactor = lowestDelta;
|
||||||
|
// Go ahead and set deltaMode to 0 since we converted to pixels
|
||||||
|
// Although this is a little odd since we overwrite the deltaX/Y
|
||||||
|
// properties with normalized deltas.
|
||||||
|
event.deltaMode = 0;
|
||||||
|
|
||||||
|
// Add event and delta to the front of the arguments
|
||||||
|
args.unshift(event, delta, deltaX, deltaY);
|
||||||
|
|
||||||
|
// Clearout lowestDelta after sometime to better
|
||||||
|
// handle multiple device types that give different
|
||||||
|
// a different lowestDelta
|
||||||
|
// Ex: trackpad = 3 and mouse wheel = 120
|
||||||
|
if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); }
|
||||||
|
nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200);
|
||||||
|
|
||||||
|
return ($.event.dispatch || $.event.handle).apply(this, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Webkit
|
function nullLowestDelta() {
|
||||||
if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
|
lowestDelta = null;
|
||||||
if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
|
}
|
||||||
|
|
||||||
// Add event and delta to the front of the arguments
|
function shouldAdjustOldDeltas(orgEvent, absDelta) {
|
||||||
args.unshift(event, delta, deltaX, deltaY);
|
// If this is an older event and the delta is divisable by 120,
|
||||||
|
// then we are assuming that the browser is treating this as an
|
||||||
|
// older mouse wheel event and that we should divide the deltas
|
||||||
|
// by 40 to try and get a more usable deltaFactor.
|
||||||
|
// Side note, this actually impacts the reported scroll distance
|
||||||
|
// in older browsers and can cause scrolling to be slower than native.
|
||||||
|
// Turn this off by setting $.event.special.mousewheel.settings.adjustOldDeltas to false.
|
||||||
|
return special.settings.adjustOldDeltas && orgEvent.type === 'mousewheel' && absDelta % 120 === 0;
|
||||||
|
}
|
||||||
|
|
||||||
return ($.event.dispatch || $.event.handle).apply(this, args);
|
}));
|
||||||
}
|
|
||||||
|
|
||||||
})(jQuery);
|
|
||||||
|
Loading…
Reference in New Issue
Block a user