Skip to content

Commit 208fc44

Browse files
committed
add passive
1 parent 6f147c2 commit 208fc44

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-scroll-anim",
3-
"version": "2.6.2",
3+
"version": "2.6.3",
44
"description": "scroll-anim anim component for react",
55
"keywords": [
66
"react",

src/EventDispatcher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function EventDispatcher(target) {
99
this._listFun = {};
1010
}
1111
EventDispatcher.prototype = {
12-
addEventListener(type, callback, target) {
12+
addEventListener(type, callback, target, options) {
1313
const types = type.split('.');
1414
const _type = types[0];
1515
const namespaces = types[1];
@@ -39,7 +39,7 @@ EventDispatcher.prototype = {
3939
if (!this._listFun[listName]) {
4040
this._listFun[listName] = this._listFun[listName] || this.dispatchEvent.bind(this, { type: _type, target });
4141
if ($target.addEventListener) {
42-
$target.addEventListener(_type, this._listFun[listName], false);
42+
$target.addEventListener(_type, this._listFun[listName], options);
4343
} else if ($target.attachEvent) {
4444
$target.attachEvent(`on${_type}`, this._listFun[listName]);
4545
}

src/ScrollScreen.jsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import easingTypes from 'tween-functions';
22
import requestAnimationFrame from 'raf';
33
import EventListener from './EventDispatcher';
4-
import { currentScrollTop } from './util';
4+
import { currentScrollTop, getPassive } from './util';
55
import mapped from './Mapped';
66

77
// 设置默认数据
@@ -15,6 +15,8 @@ function defaultData(vars) {
1515
};
1616
}
1717

18+
const passive = getPassive();
19+
1820
const ScrollScreen = {
1921
init(vars) {
2022
this.vars = defaultData(vars || {});
@@ -24,7 +26,7 @@ const ScrollScreen = {
2426
// this.currentNum = 0;
2527
['raf', 'cancelRequestAnimationFrame', 'onWheel', 'startScroll', 'isScroll']
2628
.forEach((method) => { this[method] = this[method].bind(this); });
27-
EventListener.addEventListener('wheel.scrollWheel', this.onWheel);
29+
EventListener.addEventListener('wheel.scrollWheel', this.onWheel, null, passive);
2830
// 刚进入时滚动条位置
2931
setTimeout(this.startScroll);
3032
},

src/util.js

+15
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,18 @@ export function windowHeight() {
106106

107107
export function noop() {
108108
}
109+
110+
export const getPassive = () => {
111+
let passiveSupported = false;
112+
window.addEventListener(
113+
'test',
114+
() => { },
115+
Object.defineProperty({}, 'passive', {
116+
get: () => {
117+
passiveSupported = true;
118+
return null;
119+
},
120+
}),
121+
);
122+
return passiveSupported ? { passive: false } : false;
123+
}

0 commit comments

Comments
 (0)