mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 10:44:59 +00:00
[Improvement] optimize passive events (#478)
This commit is contained in:
28
packages/utils/event.js
Normal file
28
packages/utils/event.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { isServer } from './';
|
||||
|
||||
export let supportsPassive = false;
|
||||
if (!isServer) {
|
||||
try {
|
||||
const opts = {};
|
||||
Object.defineProperty(opts, 'passive', {
|
||||
get() {
|
||||
/* istanbul ignore next */
|
||||
supportsPassive = true;
|
||||
}
|
||||
});
|
||||
window.addEventListener('test-passive', null, opts);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
export function on(target, event, handler, passive = false) {
|
||||
!isServer &&
|
||||
target.addEventListener(
|
||||
event,
|
||||
handler,
|
||||
supportsPassive ? { capture: false, passive } : false
|
||||
);
|
||||
}
|
||||
|
||||
export function off(target, event, handler) {
|
||||
!isServer && target.removeEventListener(event, handler);
|
||||
}
|
Reference in New Issue
Block a user