简体   繁体   English

在浏览器中规范鼠标滚轮速度

[英]Normalizing mousewheel speed across browsers

For a different question I composed this answer , including this sample code . 对于另一个问题,我编写了这个答案 ,包括这个示例代码

In that code I use the mouse wheel to zoom in/out of an HTML5 Canvas. 在该代码中,我使用鼠标滚轮来放大/缩小HTML5 Canvas。 I found some code that normalizes speed differences between Chrome and Firefox. 我找到了一些代码来规范Chrome和Firefox之间的速度差异。 However, the zoom handling in Safari is much, much faster than in either of those. 但是,Safari中的缩放处理要比其中任何一个快得多。

Here's the code I currently have: 这是我目前的代码:

var handleScroll = function(e){
  var delta = e.wheelDelta ? e.wheelDelta/40 : e.detail ? -e.detail/3 : 0;
  if (delta) ...
  return e.preventDefault() && false;
};
canvas.addEventListener('DOMMouseScroll',handleScroll,false); // For Firefox
canvas.addEventListener('mousewheel',handleScroll,false);     // Everyone else

What code can I use to get the same 'delta' value for the same amount of mouse wheel rolling across Chrome v10/11, Firefox v4, Safari v5, Opera v11 and IE9? 在Chrome v10 / 11,Firefox v4,Safari v5,Opera v11和IE9上滚动相同数量的鼠标滚轮时,我可以使用哪些代码来获得相同的“delta”值?

This question is related, but has no good answer. 这个问题是相关的,但没有好的答案。

Edit : Further investigation shows that one scroll event 'up' is: 编辑 :进一步调查显示一个滚动事件'up'是:

| 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

Further, using the MacBook trackpad on OS X gives different results even when moving slowly: 此外,即使在缓慢移动时,在OS X上使用MacBook触控板也会产生不同的结果:

  • On Safari and Chrome, the wheelDelta is a value of 3 instead of 120 for mouse wheel. 在Safari和Chrome上,对于鼠标滚轮, wheelDelta的值为3而不是120。
  • On Firefox the detail is usually 2 , sometimes 1 , but when scrolling very slowly NO EVENT HANDLER FIRES AT ALL . 在Firefox上, detail通常是2 ,有时是1 ,但是当滚动非常缓慢时, 没有任何事情

So the question is: 所以问题是:

What is the best way to differentiate this behavior (ideally without any user agent or OS sniffing)? 区分此行为的最佳方法是什么(理想情况下,没有任何用户代理或操作系统嗅探)?

Edit September 2014 编辑2014年9月

Given that: 鉴于:

  • Different versions of the same browser on OS X have yielded different values in the past, and may do so in the future, and that OS X上相同浏览器的不同版本在过去已经产生了不同的值,并且可能在将来也会这样做
  • Using the trackpad on OS X yields very similar effects to using a mouse wheel, yet gives very different event values , and yet the device difference cannot be detected by JS 使用OS X上的触控板产生与使用鼠标滚轮非常相似的效果 ,但却提供了非常不同的事件 ,但JS无法检测到设备差异

…I can only recommend using this simple, sign-based-counting code: ...我只能建议使用这个简单的,基于标志的计数代码:

var handleScroll = function(evt){
  if (!evt) evt = event;
  var direction = (evt.detail<0 || evt.wheelDelta>0) ? 1 : -1;
  // Use the value as you will
};
someEl.addEventListener('DOMMouseScroll',handleScroll,false); // for Firefox
someEl.addEventListener('mousewheel',    handleScroll,false); // for everyone else

Original attempt to be correct follows. 原始的尝试是正确的。

Here is my first attempt at a script to normalize the values. 这是我第一次尝试使用脚本来规范化值。 It has two flaws on OS X: Firefox on OS X will produce values 1/3 what they should be, and Chrome on OS X will produce values 1/40 what they should be. 它在OS X上有两个缺陷:OS X上的Firefox将产生1/3的值,OS X上的Chrome将产生1/40的值。

// Returns +1 for a single wheel roll 'up', -1 for a single roll 'down'
var wheelDistance = function(evt){
  if (!evt) evt = event;
  var w=evt.wheelDelta, d=evt.detail;
  if (d){
    if (w) return w/d/40*d>0?1:-1; // Opera
    else return -d/3;              // Firefox;         TODO: do not /3 for OS X
  } else return w/120;             // IE/Safari/Chrome TODO: /3 for Chrome OS X
};

You can test out this code on your own browser here: http://phrogz.net/JS/wheeldelta.html 您可以在自己的浏览器上测试此代码: http//phrogz.net/JS/wheeldelta.html

Suggestions for detecting and improving the behavior on Firefox and Chrome on OS X are welcome. 欢迎在OS X上检测和改进Firefox和Chrome行为的建议。

Edit : One suggestion from @Tom is to simply count each event call as a single move, using the sign of the distance to adjust it. 编辑 :@Tom的一个建议是简单地将每个事件调用计为一次移动,使用距离的符号进行调整。 This will not give great results under smooth/accelerated scrolling on OS X, nor handle perfectly cases when the mouse wheel is moved very fast (eg wheelDelta is 240), but these happen infrequently. 这不会在OS X上的平滑/加速滚动下产生很好的效果,也不会在鼠标滚轮移动非常快时处理完美的情况(例如wheelDelta为240),但这些情况很少发生。 This code is now the recommended technique shown at the top of this answer, for the reasons described there. 由于此处描述的原因,此代码现在是本答案顶部显示的推荐技术。

Here is my crazy attempt to produce a cross browser coherent and normalized delta ( -1 <= delta <= 1 ) : 这是我疯狂尝试生成跨浏览器相干和标准化增量(-1 <= delta <= 1):

var o = e.originalEvent,
    d = o.detail, w = o.wheelDelta,
    n = 225, n1 = n-1;

// Normalize delta
d = d ? w && (f = w/d) ? d/f : -d/1.35 : w/120;
// Quadratic scale if |d| > 1
d = d < 1 ? d < -1 ? (-Math.pow(d, 2) - n1) / n : d : (Math.pow(d, 2) + n1) / n;
// Delta *should* not be greater than 2...
e.delta = Math.min(Math.max(d / 2, -1), 1);

This is totally empirical but works quite good on Safari 6, FF 16, Opera 12 (OS X) and IE 7 on XP 这完全是经验性的,但在Safari 6,FF 16,Opera 12(OS X)和XP上的IE 7上运行良好

Our friends at Facebook put together a great solution to this problem. 我们在Facebook的朋友们为这个问题提供了一个很好的解决方案。

I have tested on a data table that I'm building using React and it scrolls like butter! 我已经在一个数据表上进行了测试,我正在使用React进行构建,它像黄油一样滚动!

This solution works on a variety of browsers, on Windows/Mac, and both using trackpad/mouse. 此解决方案适用于各种浏览器,Windows / Mac和两者都使用触控板/鼠标。

// Reasonable defaults
var PIXEL_STEP  = 10;
var LINE_HEIGHT = 40;
var PAGE_HEIGHT = 800;

function normalizeWheel(/*object*/ event) /*object*/ {
  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 };
}

The source code can be found here: https://github.com/facebook/fixed-data-table/blob/master/src/vendor_upstream/dom/normalizeWheel.js 源代码可以在这里找到: https//github.com/facebook/fixed-data-table/blob/master/src/vendor_upstream/dom/normalizeWheel.js

I made a table with different values returned by different events/browsers, taking into account the DOM3 wheel event that some browsers already support (table under). 我创建了一个包含不同事件/浏览器返回的不同值的表, 同时考虑到某些浏览器已经支持的DOM3 wheel事件(表格下)。

Based on that I made this function to normalize the speed: 基于此,我使这个功能规范化速度:

http://jsfiddle.net/mfe8J/1/ http://jsfiddle.net/mfe8J/1/

function normalizeWheelSpeed(event) {
    var normalized;
    if (event.wheelDelta) {
        normalized = (event.wheelDelta % 120 - 0) == -0 ? event.wheelDelta / 120 : event.wheelDelta / 12;
    } else {
        var rawAmmount = event.deltaY ? event.deltaY : event.detail;
        normalized = -(rawAmmount % 3 ? rawAmmount * 10 : rawAmmount / 3);
    }
    return normalized;
}

Table for mousewheel , wheel and DOMMouseScroll events: mousewheelwheelDOMMouseScroll事件表:

| mousewheel        | Chrome (win) | Chrome (mac) | Firefox (win) | Firefox (mac) | Safari 7 (mac) | Opera 22 (mac) | Opera 22 (win) | IE11      | IE 9 & 10   | IE 7 & 8  |
|-------------------|--------------|--------------|---------------|---------------|----------------|----------------|----------------|-----------|-------------|-----------|
| event.detail      | 0            | 0            | -             | -             | 0              | 0              | 0              | 0         | 0           | undefined |
| event.wheelDelta  | 120          | 120          | -             | -             | 12             | 120            | 120            | 120       | 120         | 120       |
| event.wheelDeltaY | 120          | 120          | -             | -             | 12             | 120            | 120            | undefined | undefined   | undefined |
| event.wheelDeltaX | 0            | 0            | -             | -             | 0              | 0              | 0              | undefined | undefined   | undefined |
| event.delta       | undefined    | undefined    | -             | -             | undefined      | undefined      | undefined      | undefined | undefined   | undefined |
| event.deltaY      | -100         | -4           | -             | -             | undefined      | -4             | -100           | undefined | undefined   | undefined |
| event.deltaX      | 0            | 0            | -             | -             | undefined      | 0              | 0              | undefined | undefined   | undefined |
|                   |              |              |               |               |                |                |                |           |             |           |
| wheel             | Chrome (win) | Chrome (mac) | Firefox (win) | Firefox (mac) | Safari 7 (mac) | Opera 22 (mac) | Opera 22 (win) | IE11      | IE 10 & 9   | IE 7 & 8  |
| event.detail      | 0            | 0            | 0             | 0             | -              | 0              | 0              | 0         | 0           | -         |
| event.wheelDelta  | 120          | 120          | undefined     | undefined     | -              | 120            | 120            | undefined | undefined   | -         |
| event.wheelDeltaY | 120          | 120          | undefined     | undefined     | -              | 120            | 120            | undefined | undefined   | -         |
| event.wheelDeltaX | 0            | 0            | undefined     | undefined     | -              | 0              | 0              | undefined | undefined   | -         |
| event.delta       | undefined    | undefined    | undefined     | undefined     | -              | undefined      | undefined      | undefined | undefined   | -         |
| event.deltaY      | -100         | -4           | -3            | -0,1          | -              | -4             | -100           | -99,56    | -68,4 | -53 | -         |
| event.deltaX      | 0            | 0            | 0             | 0             | -              | 0              | 0              | 0         | 0           | -         |
|                   |              |              |               |               |                |                |                |           |             |           |
|                   |              |              |               |               |                |                |                |           |             |           |
| DOMMouseScroll    |              |              | Firefox (win) | Firefox (mac) |                |                |                |           |             |           |
| event.detail      |              |              | -3            | -1            |                |                |                |           |             |           |
| event.wheelDelta  |              |              | undefined     | undefined     |                |                |                |           |             |           |
| event.wheelDeltaY |              |              | undefined     | undefined     |                |                |                |           |             |           |
| event.wheelDeltaX |              |              | undefined     | undefined     |                |                |                |           |             |           |
| event.delta       |              |              | undefined     | undefined     |                |                |                |           |             |           |
| event.deltaY      |              |              | undefined     | undefined     |                |                |                |           |             |           |
| event.deltaX      |              |              | undefined     | undefined     |                |                |                |           |             |           |

Another more or less self-contained solution... 另一种或多或少的独立解决方案......

This doesn't take time between events into account though. 但是,事件之间不需要花费时间。 Some browsers seem to always fire events with the same delta, and just fire them faster when scrolling quickly. 有些浏览器似乎总是使用相同的delta来触发事件,并且在快速滚动时只需更快地触发它们。 Others do vary the deltas. 其他人确实改变了三角洲。 One can imagine an adaptive normalizer that takes time into account, but that'd get somewhat involved and awkward to use. 人们可以想象一个需要花费时间的自适应规范化器,但这有点牵扯并且难以使用。

Working available here: jsbin/iqafek/2 可在此处工作: jsbin / iqafek / 2

var normalizeWheelDelta = function() {
  // Keep a distribution of observed values, and scale by the
  // 33rd percentile.
  var distribution = [], done = null, scale = 30;
  return function(n) {
    // Zeroes don't count.
    if (n == 0) return n;
    // After 500 samples, we stop sampling and keep current factor.
    if (done != null) return n * done;
    var abs = Math.abs(n);
    // Insert value (sorted in ascending order).
    outer: do { // Just used for break goto
      for (var i = 0; i < distribution.length; ++i) {
        if (abs <= distribution[i]) {
          distribution.splice(i, 0, abs);
          break outer;
        }
      }
      distribution.push(abs);
    } while (false);
    // Factor is scale divided by 33rd percentile.
    var factor = scale / distribution[Math.floor(distribution.length / 3)];
    if (distribution.length == 500) done = factor;
    return n * factor;
  };
}();

// Usual boilerplate scroll-wheel incompatibility plaster.

var div = document.getElementById("thing");
div.addEventListener("DOMMouseScroll", grabScroll, false);
div.addEventListener("mousewheel", grabScroll, false);

function grabScroll(e) {
  var dx = -(e.wheelDeltaX || 0), dy = -(e.wheelDeltaY || e.wheelDelta || 0);
  if (e.detail != null) {
    if (e.axis == e.HORIZONTAL_AXIS) dx = e.detail;
    else if (e.axis == e.VERTICAL_AXIS) dy = e.detail;
  }
  if (dx) {
    var ndx = Math.round(normalizeWheelDelta(dx));
    if (!ndx) ndx = dx > 0 ? 1 : -1;
    div.scrollLeft += ndx;
  }
  if (dy) {
    var ndy = Math.round(normalizeWheelDelta(dy));
    if (!ndy) ndy = dy > 0 ? 1 : -1;
    div.scrollTop += ndy;
  }
  if (dx || dy) { e.preventDefault(); e.stopPropagation(); }
}

For zoom support on touch devices, register for the gesturestart, gesturechange and gestureend events and use the event.scale property. 要获得触摸设备上的缩放支持,请注册gesturestart,gesturechange和gestureend事件,并使用event.scale属性。 You can see example code for this. 您可以看到此示例代码

For Firefox 17 the onwheel event is planned to be supported by desktop and mobile versions (as per MDN docs on onwheel ). 对于Firefox 17, onwheel事件计划由桌面和移动版本支持(根据onwheel上的MDN文档 )。 Also for Firefox maybe the Gecko specific MozMousePixelScroll event is useful (although presumably this is now deprecated since the DOMMouseWheel event is now deprecated in Firefox). 同样对于Firefox,Gecko特定的MozMousePixelScroll事件也很有用(尽管现在推荐使用它,因为现在在Firefox中不推荐使用DOMMouseWheel事件)。

For Windows, the driver itself seems to generate the WM_MOUSEWHEEL, WM_MOUSEHWHEEL events (and maybe the WM_GESTURE event for touchpad panning?). 对于Windows,驱动程序本身似乎生成WM_MOUSEWHEEL,WM_MOUSEHWHEEL事件(可能是触摸板平移的WM_GESTURE事件?)。 That would explain why Windows or the browser doesn't seem to normalise the mousewheel event values itself (and might mean you cannot write reliable code to normalise the values). 这可以解释为什么Windows或浏览器似乎没有规范化鼠标滚轮事件值本身(并且可能意味着您无法编写可靠的代码来规范化值)。

For onwheel ( not onmousewheel) event support in Internet Explorer for IE9 and IE10, you can also use the W3C standard onwheel event. 对于Internet Explorer for IE9和IE10中的onwheel onmousewheel)事件支持 ,您还可以使用W3C标准 onwheel事件。 However one notch can be a value different from 120 (eg a single notch becomes 111 (instead of -120) on my mouse using this test page ). 然而,一个凹口可以是不同于120的值(例如, 使用该测试页面 ,我的鼠标上的单个凹口变为111(而不是-120))。 I wrote another article with other details wheel events that might be relevant. 我写了另一篇文章,其中包含可能相关的其他细节轮事件。

Basically in my own testing for wheel events (I am trying to normalise the values for scrolling), I have found that I get varying values for OS, browser vendor, browser version, event type, and device (Microsoft tiltwheel mouse, laptop touchpad gestures, laptop touchpad with scrollzone, Apple magic mouse, Apple mighty mouse scrollball, Mac touchpad, etc etc). 基本上在我自己的轮子事件测试中(我试图规范化滚动值),我发现我得到的操作系统,浏览器供应商,浏览器版本,事件类型和设备(微软倾斜滚轮鼠标,笔记本电脑触摸板手势)的值不同,带有scrollzone的笔记本电脑触摸板,Apple魔力鼠标,Apple强力鼠标滚动条,Mac触摸板等等)。

And have to ignore a variety of side-effects from browser configuration (eg Firefox mousewheel.enable_pixel_scrolling, chrome --scroll-pixels=150), driver settings (eg Synaptics touchpad), and OS configuration (Windows mouse settings, OSX Mouse preferences, X.org button settings). 并且必须忽略浏览器配置中的各种副作用(例如Firefox mousewheel.enable_pixel_scrolling,chrome --scroll-pixels = 150),驱动程序设置(例如Synaptics触摸板)和操作系统配置(Windows鼠标设置,OSX鼠标首选项, X.org按钮设置)。

This is a problem I've been fighting with for some hours today, and not for the first time :( 这是我今天和我一直争吵几个小时的问题,而不是第一次:(

I've been trying to sum up values over a "swipe" and see how different browsers report values, and they vary a lot, with Safari reporting order of magnitude bigger numbers on almost all platforms, Chrome reporting quite more (like 3 times more) than firefox, firefox being balanced on the long run but quite different among platforms on small movements (on Ubuntu gnome, nearly only +3 or -3, seems like it sums up smaller events and then send a big "+3") 我一直试图通过“滑动”来总结价值,看看不同浏览器如何报告价值,并且它们变化很大,几乎所有平台上的Safari报告数量级都大一些,Chrome报告的数据更多(比如3倍) )比起firefox,firefox在长期运行中是平衡的,但在小动作的平台上差异很大(在Ubuntu gnome上,几乎只有+3或-3,似乎它总结了较小的事件,然后发送一个大的“+3”)

The current solutions found right now are three : 目前发现的解决方案有三个:

  1. The already mentioned "use only the sign" which kills any kind of acceleration 已经提到的“仅使用符号”可以杀死任何类型的加速度
  2. Sniff the browser up to minor version and platform, and adjust properly 嗅探浏览器到次要版本和平台,并正确调整
  3. Qooxdoo recently implemented a self adapting algorithm, which basically tries to scale the delta based on minimum and maximum value received so far. Qooxdoo最近实现了一种自适应算法,它基本上试图根据目前收到的最小值和最大值来缩放增量。

The idea in Qooxdoo is good, and works, and is the only solution I've currently found to be completely consistent cross browser. Qooxdoo的想法很好,而且很有效,并且是我目前发现的唯一一个完全一致的跨浏览器解决方案。

Unfortunately it tends to renormalize also the acceleration. 不幸的是,它倾向于重新规范化加速度。 If you try it (in their demos), and scroll up and down at maximum speed for a while, you'll notice that scrolling extremely fast or extremely slow basically produce nearly the same amount of movement. 如果您尝试(在他们的演示中),并以最大速度向上和向下滚动一段时间,您会注意到滚动极快或极慢基本上产生几乎相同的移动量。 On the opposite if you reload the page and only swipe very slowly, you'll notice that it will scroll quite fast". 相反,如果你重新加载页面并且只是非常缓慢地滑动,你会发现它会滚动得非常快“。

This is frustrating for a Mac user (like me) used to give vigorous scroll swipes on the touchpad and expecting to get to the top or bottom of the scrolled thing. 对于Mac用户(像我一样)而言,这对于在触摸板上进行有力的滚动滑动并期望到达滚动内容的顶部或底部是令人沮丧的。

Even more, since it scales down the mouse speed based on the maximum value obtained, the more your user tries to speed it up, the more it will slow down, while a "slow scrolling" user will experience quite fast speeds. 更重要的是,由于它根据获得的最大值缩小鼠标速度,用户尝试加速的速度越快,速度越慢,而“慢滚动”用户将体验到相当快的速度。

This makes this (otherwise brilliant) solution a slightly better implementation of solution 1. 这使得这个(其他很好的)解决方案更好地实现了解决方案1。

I ported the solution to the jquery mousewheel plugin : http://jsfiddle.net/SimoneGianni/pXzVv/ 我将解决方案移植到jquery mousewheel插件: http//jsfiddle.net/SimoneGianni/pXzVv/

If you play with it for a while, You'll see that you'll start getting quite homogeneous results, but you'll also notice that it tend to +1/-1 values quite fast. 如果你玩了一段时间,你会发现你会开始得到非常均匀的结果,但你也会注意到它倾向于+ 1 / -1值非常快。

I'm now working on enhancing it to detect peaks better, so that they don't send everything "out of scale". 我现在正在努力增强它以更好地检测峰值,这样它们就不会发送“超出规模”的所有内容。 It would also be nice to also obtain a float value between 0 and 1 as the delta value, so that there is a coherent output. 同样获得0到1之间的浮点值作为delta值也是很好的,因此存在相干输出。

There is definitely no simple way to normalize across all users in all OS in all browsers. 在所有浏览器的所有操作系统中,绝对没有简单的方法来规范所有用户。

It gets worse than your listed variations - on my WindowsXP+Firefox3.6 setup my mousewheel does 6 per one-notch scroll - probably because somewhere I've forgotten I've accelerated the mouse wheel, either in the OS or somewhere in about:config 它比你列出的变种更糟糕 - 在我的WindowsXP + Firefox3.6设置上,我的鼠标轮每个单一滚动进行6次 - 可能是因为某个地方我已经忘记了我加速了鼠标滚轮,无论是在操作系统还是在某处:配置

However I am working on a similar problem (with a similar app btw, but non-canvas) and it occurs to me by just using the delta sign of +1 / -1 and measuring over time the last time it fired, you'll have a rate of acceleration, ie. 然而,我正在研究一个类似的问题(使用类似的应用程序顺便说一句,但非画布),只有使用+1 / -1的增量符号并在上次触发时测量它,我会发生这种情况,你会有一个加速率,即。 if someone scrolls once vs several times in a few moments (which I would bet is how google maps does it). 如果有人在一段时间内滚动一次 vs 几次 (我敢打赌谷歌地图是如何做的)。

The concept seems to work well in my tests, just make anything less than 100ms add to the acceleration. 这个概念似乎在我的测试中运行良好,只需要少于100毫秒就可以加速加速。

Simple and working solution: 简单而有效的解决方案

private normalizeDelta(wheelEvent: WheelEvent):number {
    var delta = 0;
    var wheelDelta = wheelEvent.wheelDelta;
    var deltaY = wheelEvent.deltaY;
    // CHROME WIN/MAC | SAFARI 7 MAC | OPERA WIN/MAC | EDGE
    if (wheelDelta) {
        delta = -wheelDelta / 120; 
    }
    // FIREFOX WIN / MAC | IE
    if(deltaY) {
        deltaY > 0 ? delta = 1 : delta = -1;
    }
    return delta;
}
var onMouseWheel = function(e) {
    e = e.originalEvent;
    var delta = e.wheelDelta>0||e.detail<0?1:-1;
    alert(delta);
}
$("body").bind("mousewheel DOMMouseScroll", onMouseWheel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM