WheelEvent.deltaMode Scales Scroll Deltas

wheel deltas arrive in pixels, lines, or pages depending on deltaMode; multiplying by a fixed unit per mode keeps custom scrolling consistent across devices.

JavascriptData Integrity

Code

javascriptsnippet
17 lines
const LINE_HEIGHT = 16;
const PAGE_HEIGHT = 800;

function normalizeDelta(event) {
  // deltaMode units: DOM_DELTA_PIXEL (0), DOM_DELTA_LINE (1), DOM_DELTA_PAGE (2)
  if (event.deltaMode === event.DOM_DELTA_LINE) return event.deltaY * LINE_HEIGHT;
  if (event.deltaMode === event.DOM_DELTA_PAGE) return event.deltaY * PAGE_HEIGHT;
  return event.deltaY;
}

// Classic mice report line deltas while trackpad momentum reports pixels,
// which is exactly the mismatch this normalization removes.

window.addEventListener('wheel', (event) => {
  const pixels = normalizeDelta(event);
  console.log(`deltaMode ${event.deltaMode}: deltaY ${event.deltaY} -> ${Math.round(pixels)}px`);
});

The shape of this snippet — WheelEvent.deltaMode Scales Scroll Deltas

Everything happens inside the function normalizeDelta.

What to watch for

  • Zero installs — the platform’s standard library backs each and every call the file makes.
  • window exist only in browsers — a Node runtime has no shims for them out of the box.
  • The guard event.deltaMode === event.DOM_DELTA_LINE exits the current pass of the loop early via return when its condition holds.

Applying it

Signature: function normalizeDelta(event)

Statements that matter

window.addEventListener('wheel', (event) => {
  const pixels = normalizeDelta(event);
  console.log(`deltaMode ${event.deltaMode}: deltaY ${event.deltaY} -> ${Math.round(pixels)}px`);
});
if (event.deltaMode === event.DOM_DELTA_LINE) return event.deltaY * LINE_HEIGHT;
if (event.deltaMode === event.DOM_DELTA_PAGE) return event.deltaY * PAGE_HEIGHT;

Interactive Tools for This Topic

FAQ

Does WheelEvent.deltaMode Scales Scroll Deltas run in plain Node.js?
No — it touches `window`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.