Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Normalize mouse wheel deltas (almende#3911)
Browse files Browse the repository at this point in the history
* Normalize mouse wheel deltas for almende#3792

* include support for IE6/7/8
  • Loading branch information
wildyeast authored and mojoaxel committed Jun 9, 2019
1 parent ca2fa1d commit 633f498
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/timeline/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ Core.prototype._create = function (container) {
* @param {WheelEvent} event
*/
function onMouseWheel(event) {

// Reasonable default wheel deltas
const LINE_HEIGHT = 40;
const PAGE_HEIGHT = 800;

if (this.isActive()) {
this.emit('mousewheel', event);
}
Expand Down Expand Up @@ -204,6 +209,17 @@ Core.prototype._create = function (container) {
deltaX = event.deltaX;
}

// Normalize deltas
if (event.deltaMode) {
if (event.deltaMode === 1) { // delta in LINE units
deltaX *= LINE_HEIGHT;
deltaY *= LINE_HEIGHT;
} else { // delta in PAGE units
deltaX *= LINE_HEIGHT;
deltaY *= PAGE_HEIGHT;
}
}

// Prevent scrolling when zooming (no zoom key, or pressing zoom key)
if (!this.options.zoomKey || event[this.options.zoomKey]) return;

Expand Down Expand Up @@ -241,11 +257,12 @@ Core.prototype._create = function (container) {
}
}

// Add modern wheel event listener
if (this.dom.centerContainer.addEventListener) {
// IE9, Chrome, Safari, Opera
this.dom.centerContainer.addEventListener("mousewheel", onMouseWheel.bind(this), false);
// Firefox
this.dom.centerContainer.addEventListener("DOMMouseScroll", onMouseWheel.bind(this), false);
const wheel = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
"DOMMouseScroll"; // Older Firefox versions like "DOMMouseScroll"
this.dom.centerContainer.addEventListener(wheel, onMouseWheel.bind(this), false);
} else {
// IE 6/7/8
this.dom.centerContainer.attachEvent("onmousewheel", onMouseWheel.bind(this));
Expand Down

0 comments on commit 633f498

Please sign in to comment.