提交 | 用户 | 时间
|
1ac2bc
|
1 |
/*! |
懒 |
2 |
* jQuery Mousewheel 3.1.13 |
|
3 |
* |
|
4 |
* Copyright jQuery Foundation and other contributors |
|
5 |
* Released under the MIT license |
|
6 |
* http://jquery.org/license |
|
7 |
*/ |
|
8 |
|
|
9 |
(function (factory) { |
|
10 |
if (typeof define === 'function' && define.amd) { |
|
11 |
// AMD. Register as an anonymous module. |
|
12 |
define(['jquery'], factory); |
|
13 |
} else if (typeof exports === 'object') { |
|
14 |
// Node/CommonJS style for Browserify |
|
15 |
module.exports = factory; |
|
16 |
} else if (window.layui && layui.define) { // layui加载 |
|
17 |
layui.define('jquery', function (exports) { |
|
18 |
var $ = layui.jquery; |
|
19 |
exports('mousewheel', factory($)); |
|
20 |
}); |
|
21 |
} else { |
|
22 |
// Browser globals |
|
23 |
factory(jQuery); |
|
24 |
} |
|
25 |
}(function ($) { |
|
26 |
|
|
27 |
var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'], |
|
28 |
toBind = ('onwheel' in document || document.documentMode >= 9) ? |
|
29 |
['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'], |
|
30 |
slice = Array.prototype.slice, |
|
31 |
nullLowestDeltaTimeout, lowestDelta; |
|
32 |
|
|
33 |
if ($.event.fixHooks) { |
|
34 |
for (var i = toFix.length; i;) { |
|
35 |
$.event.fixHooks[toFix[--i]] = $.event.mouseHooks; |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
var special = $.event.special.mousewheel = { |
|
40 |
version: '3.1.12', |
|
41 |
|
|
42 |
setup: function () { |
|
43 |
if (this.addEventListener) { |
|
44 |
for (var i = toBind.length; i;) { |
|
45 |
this.addEventListener(toBind[--i], handler, false); |
|
46 |
} |
|
47 |
} else { |
|
48 |
this.onmousewheel = handler; |
|
49 |
} |
|
50 |
// Store the line height and page height for this particular element |
|
51 |
$.data(this, 'mousewheel-line-height', special.getLineHeight(this)); |
|
52 |
$.data(this, 'mousewheel-page-height', special.getPageHeight(this)); |
|
53 |
}, |
|
54 |
|
|
55 |
teardown: function () { |
|
56 |
if (this.removeEventListener) { |
|
57 |
for (var i = toBind.length; i;) { |
|
58 |
this.removeEventListener(toBind[--i], handler, false); |
|
59 |
} |
|
60 |
} else { |
|
61 |
this.onmousewheel = null; |
|
62 |
} |
|
63 |
// Clean up the data we added to the element |
|
64 |
$.removeData(this, 'mousewheel-line-height'); |
|
65 |
$.removeData(this, 'mousewheel-page-height'); |
|
66 |
}, |
|
67 |
|
|
68 |
getLineHeight: function (elem) { |
|
69 |
var $elem = $(elem), |
|
70 |
$parent = $elem['offsetParent' in $.fn ? 'offsetParent' : 'parent'](); |
|
71 |
if (!$parent.length) { |
|
72 |
$parent = $('body'); |
|
73 |
} |
|
74 |
return parseInt($parent.css('fontSize'), 10) || parseInt($elem.css('fontSize'), 10) || 16; |
|
75 |
}, |
|
76 |
|
|
77 |
getPageHeight: function (elem) { |
|
78 |
return $(elem).height(); |
|
79 |
}, |
|
80 |
|
|
81 |
settings: { |
|
82 |
adjustOldDeltas: true, // see shouldAdjustOldDeltas() below |
|
83 |
normalizeOffset: true // calls getBoundingClientRect for each event |
|
84 |
} |
|
85 |
}; |
|
86 |
|
|
87 |
$.fn.extend({ |
|
88 |
mousewheel: function (fn) { |
|
89 |
return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel'); |
|
90 |
}, |
|
91 |
|
|
92 |
unmousewheel: function (fn) { |
|
93 |
return this.unbind('mousewheel', fn); |
|
94 |
} |
|
95 |
}); |
|
96 |
|
|
97 |
|
|
98 |
function handler(event) { |
|
99 |
var orgEvent = event || window.event, |
|
100 |
args = slice.call(arguments, 1), |
|
101 |
delta = 0, |
|
102 |
deltaX = 0, |
|
103 |
deltaY = 0, |
|
104 |
absDelta = 0, |
|
105 |
offsetX = 0, |
|
106 |
offsetY = 0; |
|
107 |
event = $.event.fix(orgEvent); |
|
108 |
event.type = 'mousewheel'; |
|
109 |
|
|
110 |
// Old school scrollwheel delta |
|
111 |
if ('detail' in orgEvent) { |
|
112 |
deltaY = orgEvent.detail * -1; |
|
113 |
} |
|
114 |
if ('wheelDelta' in orgEvent) { |
|
115 |
deltaY = orgEvent.wheelDelta; |
|
116 |
} |
|
117 |
if ('wheelDeltaY' in orgEvent) { |
|
118 |
deltaY = orgEvent.wheelDeltaY; |
|
119 |
} |
|
120 |
if ('wheelDeltaX' in orgEvent) { |
|
121 |
deltaX = orgEvent.wheelDeltaX * -1; |
|
122 |
} |
|
123 |
|
|
124 |
// Firefox < 17 horizontal scrolling related to DOMMouseScroll event |
|
125 |
if ('axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS) { |
|
126 |
deltaX = deltaY * -1; |
|
127 |
deltaY = 0; |
|
128 |
} |
|
129 |
|
|
130 |
// Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy |
|
131 |
delta = deltaY === 0 ? deltaX : deltaY; |
|
132 |
|
|
133 |
// New school wheel delta (wheel event) |
|
134 |
if ('deltaY' in orgEvent) { |
|
135 |
deltaY = orgEvent.deltaY * -1; |
|
136 |
delta = deltaY; |
|
137 |
} |
|
138 |
if ('deltaX' in orgEvent) { |
|
139 |
deltaX = orgEvent.deltaX; |
|
140 |
if (deltaY === 0) { |
|
141 |
delta = deltaX * -1; |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
// No change actually happened, no reason to go any further |
|
146 |
if (deltaY === 0 && deltaX === 0) { |
|
147 |
return; |
|
148 |
} |
|
149 |
|
|
150 |
// Need to convert lines and pages to pixels if we aren't already in pixels |
|
151 |
// There are three delta modes: |
|
152 |
// * deltaMode 0 is by pixels, nothing to do |
|
153 |
// * deltaMode 1 is by lines |
|
154 |
// * deltaMode 2 is by pages |
|
155 |
if (orgEvent.deltaMode === 1) { |
|
156 |
var lineHeight = $.data(this, 'mousewheel-line-height'); |
|
157 |
delta *= lineHeight; |
|
158 |
deltaY *= lineHeight; |
|
159 |
deltaX *= lineHeight; |
|
160 |
} else if (orgEvent.deltaMode === 2) { |
|
161 |
var pageHeight = $.data(this, 'mousewheel-page-height'); |
|
162 |
delta *= pageHeight; |
|
163 |
deltaY *= pageHeight; |
|
164 |
deltaX *= pageHeight; |
|
165 |
} |
|
166 |
|
|
167 |
// Store lowest absolute delta to normalize the delta values |
|
168 |
absDelta = Math.max(Math.abs(deltaY), Math.abs(deltaX)); |
|
169 |
|
|
170 |
if (!lowestDelta || absDelta < lowestDelta) { |
|
171 |
lowestDelta = absDelta; |
|
172 |
|
|
173 |
// Adjust older deltas if necessary |
|
174 |
if (shouldAdjustOldDeltas(orgEvent, absDelta)) { |
|
175 |
lowestDelta /= 40; |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
// Adjust older deltas if necessary |
|
180 |
if (shouldAdjustOldDeltas(orgEvent, absDelta)) { |
|
181 |
// Divide all the things by 40! |
|
182 |
delta /= 40; |
|
183 |
deltaX /= 40; |
|
184 |
deltaY /= 40; |
|
185 |
} |
|
186 |
|
|
187 |
// Get a whole, normalized value for the deltas |
|
188 |
delta = Math[delta >= 1 ? 'floor' : 'ceil'](delta / lowestDelta); |
|
189 |
deltaX = Math[deltaX >= 1 ? 'floor' : 'ceil'](deltaX / lowestDelta); |
|
190 |
deltaY = Math[deltaY >= 1 ? 'floor' : 'ceil'](deltaY / lowestDelta); |
|
191 |
|
|
192 |
// Normalise offsetX and offsetY properties |
|
193 |
if (special.settings.normalizeOffset && this.getBoundingClientRect) { |
|
194 |
var boundingRect = this.getBoundingClientRect(); |
|
195 |
offsetX = event.clientX - boundingRect.left; |
|
196 |
offsetY = event.clientY - boundingRect.top; |
|
197 |
} |
|
198 |
|
|
199 |
// Add information to the event object |
|
200 |
event.deltaX = deltaX; |
|
201 |
event.deltaY = deltaY; |
|
202 |
event.deltaFactor = lowestDelta; |
|
203 |
event.offsetX = offsetX; |
|
204 |
event.offsetY = offsetY; |
|
205 |
// Go ahead and set deltaMode to 0 since we converted to pixels |
|
206 |
// Although this is a little odd since we overwrite the deltaX/Y |
|
207 |
// properties with normalized deltas. |
|
208 |
event.deltaMode = 0; |
|
209 |
|
|
210 |
// Add event and delta to the front of the arguments |
|
211 |
args.unshift(event, delta, deltaX, deltaY); |
|
212 |
|
|
213 |
// Clearout lowestDelta after sometime to better |
|
214 |
// handle multiple device types that give different |
|
215 |
// a different lowestDelta |
|
216 |
// Ex: trackpad = 3 and mouse wheel = 120 |
|
217 |
if (nullLowestDeltaTimeout) { |
|
218 |
clearTimeout(nullLowestDeltaTimeout); |
|
219 |
} |
|
220 |
nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200); |
|
221 |
|
|
222 |
return ($.event.dispatch || $.event.handle).apply(this, args); |
|
223 |
} |
|
224 |
|
|
225 |
function nullLowestDelta() { |
|
226 |
lowestDelta = null; |
|
227 |
} |
|
228 |
|
|
229 |
function shouldAdjustOldDeltas(orgEvent, absDelta) { |
|
230 |
// If this is an older event and the delta is divisable by 120, |
|
231 |
// then we are assuming that the browser is treating this as an |
|
232 |
// older mouse wheel event and that we should divide the deltas |
|
233 |
// by 40 to try and get a more usable deltaFactor. |
|
234 |
// Side note, this actually impacts the reported scroll distance |
|
235 |
// in older browsers and can cause scrolling to be slower than native. |
|
236 |
// Turn this off by setting $.event.special.mousewheel.settings.adjustOldDeltas to false. |
|
237 |
return special.settings.adjustOldDeltas && orgEvent.type === 'mousewheel' && absDelta % 120 === 0; |
|
238 |
} |
|
239 |
|
|
240 |
})); |