Skip to content

Add support for passive event listeners #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/jquery.mousewheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,43 @@
version: "@VERSION",

setup: function() {
this.addEventListener( "wheel", handler, false );
// Use passive event listeners if supported
var passiveSupported = false;
try {
var opts = Object.defineProperty({}, "passive", {
get: function() {
passiveSupported = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}
this.addEventListener(
"wheel",
handler,
passiveSupported ? { passive: true } : false
);

// Store the line height and page height for this particular element
$.data( this, "mousewheel-line-height", special.getLineHeight( this ) );
$.data( this, "mousewheel-page-height", special.getPageHeight( this ) );
},

teardown: function() {
this.removeEventListener( "wheel", handler, false );
// Use passive event listeners if supported
var passiveSupported = false;
try {
var opts = Object.defineProperty({}, "passive", {
get: function() {
passiveSupported = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}
this.removeEventListener(
"wheel",
handler,
passiveSupported ? { passive: true } : false
);

// Clean up the data we added to the element
$.removeData( this, "mousewheel-line-height" );
Expand Down