1.滚轮相关事件
事件类型 | 事件对象 | 是否标准化 | 兼容性 |
---|
mousewheel | MouseWheelEvent | 非标准化 | 只有Firefox不支持 |
DOMMouseScroll | MouseScrollEvent | 非标准化 | 只有Firefox支持 |
wheel | WheelEvent | DOM Level 3 | Firefox 17+/ie9+ |
wheel事件详细内容:wheel事件
2.通用滚轮事件(wheel)
浏览器类型 | evt.wheelDelta | evt.detail |
---|
Safari v5/Win7 | 120 | 0 |
Safari v5/OS X | 120 | 0 |
Safari v7/OS X | 12 | 0 |
Chrome v11/Win7 | 120 | 0 |
Chrome v37/Win7 | 120 | 0 |
Chrome v11/OS X | 3(!) | 0(possibly wrong) |
Chrome v37/OS X | 120 | 0 |
IE9/Win7 | 120 | undefined |
Opera v11/OS X | 40 | -1 |
Opera v24/OS X | 120 | 0 |
Opera v11/Win7 | 120 | -3 |
Firefox v4/Win7 | undefined | -3 |
Firefox v4/OS X | undefined | -1 |
Firefox v30/OS X | undefined | -1 |
'use strict';
var UserAgent_DEPRECATED = require('UserAgent_DEPRECATED');
var isEventSupported = require('isEventSupported');
// Reasonable defaults
var PIXEL_STEP = 10;
var LINE_HEIGHT = 40;
var PAGE_HEIGHT = 800;
function normalizeWheel(event) {
var sX = 0, sY = 0, // spinX, spinY
pX = 0, pY = 0; // pixelX, pixelY
// Legacy
if ('detail' in event) { sY = event.detail; }
if ('wheelDelta' in event) { sY = -event.wheelDelta / 120; }
if ('wheelDeltaY' in event) { sY = -event.wheelDeltaY / 120; }
if ('wheelDeltaX' in event) { sX = -event.wheelDeltaX / 120; }
// side scrolling on FF with DOMMouseScroll
if ( 'axis' in event && event.axis === event.HORIZONTAL_AXIS ) {
sX = sY;
sY = 0;
}
pX = sX * PIXEL_STEP;
pY = sY * PIXEL_STEP;
if ('deltaY' in event) { pY = event.deltaY; }
if ('deltaX' in event) { pX = event.deltaX; }
if ((pX || pY) && event.deltaMode) {
if (event.deltaMode == 1) { // delta in LINE units
pX *= LINE_HEIGHT;
pY *= LINE_HEIGHT;
} else { // delta in PAGE units
pX *= PAGE_HEIGHT;
pY *= PAGE_HEIGHT;
}
}
// Fall-back if spin cannot be determined
if (pX && !sX) { sX = (pX < 1) ? -1 : 1; }
if (pY && !sY) { sY = (pY < 1) ? -1 : 1; }
return { spinX : sX,
spinY : sY,
pixelX : pX,
pixelY : pY };
}
/*spinX + spinY标准化的最佳组合。 它支持旧的DOMMouseScroll for Firefox,因为FF不包含带有'wheel'事件的wheelDelta,因此无法确定旋转速度。*/
normalizeWheel.getEventType = function() /*string*/ {
return (UserAgent_DEPRECATED.firefox())
? 'DOMMouseScroll'
: (isEventSupported('wheel'))
? 'wheel'
: 'mousewheel';
};
module.exports = normalizeWheel;