Skip to content

Commit 93c18d1

Browse files
committed
Add support for passive event listeners
1 parent 9f32b90 commit 93c18d1

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/jquery.mousewheel.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,43 @@
3636
version: "@VERSION",
3737

3838
setup: function() {
39-
this.addEventListener( "wheel", handler, false );
39+
// Use passive event listeners if supported
40+
var passiveSupported = false;
41+
try {
42+
var opts = Object.defineProperty({}, "passive", {
43+
get: function() {
44+
passiveSupported = true;
45+
}
46+
});
47+
window.addEventListener("test", null, opts);
48+
} catch (e) {}
49+
this.addEventListener(
50+
"wheel",
51+
handler,
52+
passiveSupported ? { passive: true } : false
53+
);
4054

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

4660
teardown: function() {
47-
this.removeEventListener( "wheel", handler, false );
61+
// Remove with same passive option as setup
62+
var passiveSupported = false;
63+
try {
64+
var opts = Object.defineProperty({}, "passive", {
65+
get: function() {
66+
passiveSupported = true;
67+
}
68+
});
69+
window.addEventListener("test", null, opts);
70+
} catch (e) {}
71+
this.removeEventListener(
72+
"wheel",
73+
handler,
74+
passiveSupported ? { passive: true } : false
75+
);
4876

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

0 commit comments

Comments
 (0)