Skip to content

Commit 873086b

Browse files
committed
1 parent cd45ca8 commit 873086b

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

examples/scrollScreen.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class Demo extends React.Component {
8282
</div>
8383
<Element className="pack-page page0" id="page0">
8484
<QueueAnim className="home-title">
85-
<div className="page-title" key="title">
86-
<p>{_package.name}@{_package.version}</p>
85+
<div className="page-title" key="title" style={{ height: 100, overflow: 'auto' }}>
86+
<p style={{ height: 600 }}>{_package.name}@{_package.version}</p>
8787
</div>
8888
<div className="page-description" key="c">
8989
<p>A scrollScreen demo</p>
@@ -100,7 +100,7 @@ class Demo extends React.Component {
100100
<TweenOne className="tween-one" key="0" animation={{ opacity: 1 }}>
101101
每次进入都启动播放
102102
</TweenOne>
103-
<QueueAnim key="1">
103+
<QueueAnim key="1" style={{ height: 100 }}>
104104
<div key="0" className="demo"></div>
105105
<div key="1" className="demo"></div>
106106
<div key="2" className="demo"></div>
@@ -110,8 +110,8 @@ class Demo extends React.Component {
110110

111111
<ScrollOverPack
112112
className="pack-page page2"
113-
style={{ backgroundColor: '#174270' }}
114-
id="page3"
113+
style={{ backgroundColor: '#174270', height: 500 }}
114+
id="page2"
115115
playScale={1}
116116
hideProps={{ t: { reverse: true }, 1: { reverse: true } }}
117117
>
@@ -133,7 +133,7 @@ class Demo extends React.Component {
133133
className="pack-page page3"
134134
style={{ backgroundColor: '#174270' }}
135135
always={false}
136-
id="page2"
136+
id="page3"
137137
playScale={1}
138138
hideProps={{ t: { reverse: true }, 1: { reverse: true } }}
139139
>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-scroll-anim",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"description": "scroll-anim anim component for react",
55
"keywords": [
66
"react",

src/ScrollScreen.jsx

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,28 @@ const ScrollScreen = {
2222
this.toHeight = -1;
2323
this.num = 0;
2424
// this.currentNum = 0;
25-
['raf', 'cancelRequestAnimationFrame', 'onWheel', 'startScroll']
25+
['raf', 'cancelRequestAnimationFrame', 'onWheel', 'startScroll', 'isScroll']
2626
.forEach((method) => this[method] = this[method].bind(this));
2727
EventListener.addEventListener('wheel.scrollWheel', this.onWheel);
28-
EventListener.addEventListener('scroll.scrollScreen', this.scrollEvent);
2928
// 刚进入时滚动条位置
30-
// requestAnimationFrame(this.startScroll)
3129
setTimeout(this.startScroll);
3230
},
33-
scrollEvent() {
34-
const _mapped = mapped.getMapped();
35-
const _arr = _mapped.__arr;
36-
this.scrollTop = currentScrollTop();
37-
_arr.forEach((str, i) => {
38-
const dom = _mapped[str];
39-
const domOffsetTop = dom.offsetTop;
40-
const domHeight = dom.getBoundingClientRect().height;
41-
if (this.scrollTop >= domOffsetTop && this.scrollTop < domOffsetTop + domHeight) {
42-
this.currentNum = i;
43-
}
44-
});
45-
},
31+
4632
startScroll() {
4733
const _mapped = mapped.getMapped();
4834
const _arr = _mapped.__arr;
4935
if (!_arr.length) {
5036
EventListener.removeEventListener('wheel.scrollWheel', this.onWheel);
5137
return;
5238
}
53-
this.scrollTop = window.pageYOffset;
39+
this.scrollTop = currentScrollTop();
5440
_arr.forEach((str, i) => {
5541
const dom = _mapped[str];
5642
const domOffsetTop = dom.offsetTop;
5743
const domHeight = dom.getBoundingClientRect().height;
5844
if (this.scrollTop >= domOffsetTop && this.scrollTop < domOffsetTop + domHeight) {
5945
this.num = i;
6046
this.toHeight = domOffsetTop;
61-
// this.currentNum = this.num;
6247
}
6348
});
6449
// 如果 toHeight === -1 且 this.scrollTop 有值时;
@@ -69,7 +54,6 @@ const ScrollScreen = {
6954
const tooNum = Math.ceil((this.scrollTop - endDom.offsetTop -
7055
endDom.getBoundingClientRect().height) / windowHeight);
7156
this.num = mapped.getMapped().__arr.length + tooNum;
72-
// this.currentNum = this.num;
7357
}
7458
return;
7559
}
@@ -100,15 +84,35 @@ const ScrollScreen = {
10084
requestAnimationFrame.cancel(this.rafID);
10185
this.rafID = -1;
10286
},
87+
getComputedStyle(dom) {
88+
return document.defaultView ? document.defaultView.getComputedStyle(dom) : {};
89+
},
90+
isScroll(dom) {
91+
const style = this.getComputedStyle(dom);
92+
const overflow = style.overflow;
93+
const overflowY = style.overflowY;
94+
const isScrollOverflow = overflow === 'auto' || overflow === 'scroll' || overflow === 'overlay'
95+
|| overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'overlay';
96+
if (dom === document.body) {
97+
return false;
98+
} else if (dom.scrollHeight > dom.offsetHeight
99+
&& isScrollOverflow
100+
&& dom.scrollTop < dom.scrollHeight) {
101+
return true;
102+
}
103+
return this.isScroll(dom.parentNode);
104+
},
103105
onWheel(e) {
104106
const _mapped = mapped.getMapped();
105107
if (!_mapped.__arr.length) {
106108
EventListener.removeEventListener('wheel.scrollWheel', this.onWheel);
107109
return;
108110
}
111+
if (this.isScroll(e.target)) {
112+
return;
113+
}
109114
const deltaY = e.deltaY;
110115
e.preventDefault();
111-
// console.log(e.wheelDelta,e.deltaY)
112116
if (this.rafID === -1 && deltaY !== 0 && this.toHeight === -1) {
113117
// 如果滚动条托动过了,需要获取当前的num;
114118
const _arr = _mapped.__arr;
@@ -165,14 +169,13 @@ const ScrollScreen = {
165169
windowHeight * (this.num - mapped.getMapped().__arr.length) : this.toHeight;
166170
this.toHeight = this.toHeight < 0 ? 0 : this.toHeight;
167171
this.toHeight = this.toHeight > docHeight - windowHeight ?
168-
docHeight - windowHeight : this.toHeight;
172+
(docHeight - windowHeight) : this.toHeight;
169173
this.rafID = requestAnimationFrame(this.raf);
170-
// this.currentNum = this.num;
174+
this.currentNum = this.num;
171175
}
172176
},
173177
unMount() {
174178
EventListener.removeEventListener('wheel.scrollWheel', this.onWheel);
175-
EventListener.removeEventListener('scroll.scrollScreen', this.scrollEvent);
176179
},
177180
};
178181
export default {

src/util.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ export function objectEqual(obj1, obj2) {
9191
}
9292

9393
export function currentScrollTop() {
94-
const supportPageOffset = window.pageXOffset !== undefined;
95-
const isCSS1Compat = ((document.compatMode || '') === 'CSS1Compat');
96-
const isCSS1ScrollTop = isCSS1Compat ?
97-
document.documentElement.scrollTop : document.body.scrollTop;
98-
return supportPageOffset ? window.pageYOffset : isCSS1ScrollTop;
94+
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
9995
}
10096

10197
export function windowHeight() {

0 commit comments

Comments
 (0)