detect-passive-events

Detect if the browser supports passive events

MIT 10 个版本
安装
npm install detect-passive-events
yarn add detect-passive-events
pnpm add detect-passive-events
bun add detect-passive-events
README

Detect Passive Events

Detects if the browser supports passive event listeners. Tree-shakable and side-effect free. Also available as part of detect-it.

Live detection test

npm npm bundle size npm type definitions

Note that the code used in the detection is adapted from this Passive Events Explainer.

Using detect-passive-events

npm install --save detect-passive-events
// supportsPassiveEvents is a boolean
import { supportsPassiveEvents } from 'detect-passive-events';

if (supportsPassiveEvents) {
  // passive events are supported by the browser
  document.addEventListener('scroll', handleScroll, { capture: false, passive: true });
} else {
  // passive events are not supported by the browser
  document.addEventListener('scroll', handleScroll, false);
}

Or use the script directly in the browser

Optionally, instead using npm install you can the load the script directly in the browser. A minified UMD version is available from Unpkg for this purpose.

<script src="https://unpkg.com/detect-passive-events@2/dist/detect-passive-events.umd.production.js"></script>
// it will be available on the window as DetectPassiveEvents
if (window.DetectPassiveEvents.supportsPassiveEvents) {
  document.addEventListener('scroll', handleScroll, { capture: false, passive: true });
} else {
  document.addEventListener('scroll', handleScroll, false);
}
版本列表
2.0.3 2021-02-12
2.0.2 2020-11-27
2.0.1 2020-10-15
2.0.0 2020-10-15
1.0.5 2020-09-10
1.0.4 2017-09-26
1.0.3 2017-09-25
1.0.2 2017-02-27
1.0.1 2017-02-24
1.0.0 2016-11-08