Skip to content

Commit 3afddef

Browse files
committed
Add feature detection for passive event listeners
1 parent 1c39343 commit 3afddef

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

js/waves.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
var toString = Object.prototype.toString;
4444
var isTouchAvailable = 'ontouchstart' in window;
4545

46+
/* Feature detection */
47+
var passiveIfSupported = false;
48+
try {
49+
window.addEventListener("test", null,
50+
Object.defineProperty({}, "passive", {
51+
get: function() { passiveIfSupported = { passive: false }; }
52+
}
53+
));
54+
} catch(err) {}
4655

4756
// Find exact position of element
4857
function isWindow(obj) {
@@ -446,9 +455,9 @@
446455
removeListeners();
447456
};
448457

449-
element.addEventListener('touchmove', touchMove, { capture:false, passive: true });
450-
element.addEventListener('touchend', hideEffect, { capture:false, passive: true });
451-
element.addEventListener('touchcancel', hideEffect, { capture:false, passive: true });
458+
element.addEventListener('touchmove', touchMove, passiveIfSupported);
459+
element.addEventListener('touchend', hideEffect, passiveIfSupported);
460+
element.addEventListener('touchcancel', hideEffect, passiveIfSupported);
452461

453462
var removeListeners = function() {
454463
element.removeEventListener('touchmove', touchMove);
@@ -460,12 +469,12 @@
460469
Effect.show(e, element);
461470

462471
if (isTouchAvailable) {
463-
element.addEventListener('touchend', Effect.hide, { capture:false, passive: true });
464-
element.addEventListener('touchcancel', Effect.hide, { capture:false, passive: true });
472+
element.addEventListener('touchend', Effect.hide, passiveIfSupported);
473+
element.addEventListener('touchcancel', Effect.hide, passiveIfSupported);
465474
}
466475

467-
element.addEventListener('mouseup', Effect.hide, { capture:false, passive: true });
468-
element.addEventListener('mouseleave', Effect.hide, { capture:false, passive: true });
476+
element.addEventListener('mouseup', Effect.hide, passiveIfSupported);
477+
element.addEventListener('mouseleave', Effect.hide, passiveIfSupported);
469478
}
470479
}
471480
}
@@ -484,12 +493,12 @@
484493
}
485494

486495
if (isTouchAvailable) {
487-
body.addEventListener('touchstart', showEffect, { capture:false, passive: true });
488-
body.addEventListener('touchcancel', TouchHandler.registerEvent, { capture:false, passive: true });
489-
body.addEventListener('touchend', TouchHandler.registerEvent, { capture:false, passive: true });
496+
body.addEventListener('touchstart', showEffect, passiveIfSupported);
497+
body.addEventListener('touchcancel', TouchHandler.registerEvent, passiveIfSupported);
498+
body.addEventListener('touchend', TouchHandler.registerEvent, passiveIfSupported);
490499
}
491500

492-
body.addEventListener('mousedown', showEffect, { capture:false, passive: true });
501+
body.addEventListener('mousedown', showEffect, passiveIfSupported);
493502
};
494503

495504

0 commit comments

Comments
 (0)