Skip to content

Commit a135aed

Browse files
committed
update 1.0.0
1 parent 791af25 commit a135aed

File tree

8 files changed

+68
-48
lines changed

8 files changed

+68
-48
lines changed

examples/link.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Demo extends React.Component {
8585
</div>
8686
</QueueAnim>
8787
</Element>
88-
<ScrollOverPack id="page1" className="page1" hideProps={{ 0: { reverse: true } }}>
88+
<ScrollOverPack id="page1" className="page1">
8989
<TweenOne className="tween-one" key="0" animation={{ opacity: 1 }}>
9090
默认进入与出场
9191
</TweenOne>
@@ -102,7 +102,6 @@ class Demo extends React.Component {
102102
style={{ backgroundColor: '#0098CE' }}
103103
always={false}
104104
id="page2"
105-
hideProps={{ 1: { reverse: true } }}
106105
>
107106
<div className="page2-title">只进入一次</div>
108107
<Animate key="0" transitionName="fade" transitionAppear>
@@ -121,7 +120,6 @@ class Demo extends React.Component {
121120
style={{ backgroundColor: '#174270' }}
122121
playScale={0.8}
123122
id="page3"
124-
hideProps={{ title: { reverse: true }, 1: { reverse: true } }}
125123
>
126124
<TweenOne animation={{ opacity: 1 }} style={{ opacity: 0 }} key="title"
127125
className="page2-title"

examples/linkAsynchronous.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Demo extends React.Component {
9090
</div>
9191
</QueueAnim>
9292
</Element>,
93-
<ScrollOverPack id="page1" className="page1" hideProps={{ 0: { reverse: true } }} key="1">
93+
<ScrollOverPack id="page1" className="page1" key="1">
9494
<TweenOne className="tween-one" key="0" animation={{ opacity: 1 }}>
9595
默认进入与出场
9696
</TweenOne>
@@ -107,7 +107,6 @@ class Demo extends React.Component {
107107
style={{ backgroundColor: '#0098CE' }}
108108
always={false}
109109
id="page2"
110-
hideProps={{ 1: { reverse: true } }}
111110
key="2"
112111
>
113112
<div className="page2-title">只进入一次</div>
@@ -127,7 +126,6 @@ class Demo extends React.Component {
127126
style={{ backgroundColor: '#174270' }}
128127
playScale={0.8}
129128
id="page3"
130-
hideProps={{ title: { reverse: true }, 1: { reverse: true } }}
131129
key="3"
132130
>
133131
<TweenOne animation={{ opacity: 1 }} style={{ opacity: 0 }} key="title"

examples/scrollScreen.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ class Demo extends React.Component {
9595
className="page1"
9696
playScale={1}
9797
replay
98-
hideProps={{ 0: { reverse: true } }}
9998
location="page1"
10099
style={{ marginTop: 150 }}
101100
>
@@ -116,7 +115,6 @@ class Demo extends React.Component {
116115
style={{ backgroundColor: '#174270', height: 500 }}
117116
id="page2"
118117
playScale={1}
119-
hideProps={{ t: { reverse: true }, 1: { reverse: true } }}
120118
>
121119
<TweenOne className="tween-one" animation={{ opacity: 1 }} key="t">
122120
只从上往下时播放
@@ -138,7 +136,6 @@ class Demo extends React.Component {
138136
always={false}
139137
id="page3"
140138
playScale={1}
141-
hideProps={{ t: { reverse: true }, 1: { reverse: true } }}
142139
>
143140
<TweenOne
144141
animation={{ opacity: 1 }}

examples/simple.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@ class Demo extends React.Component {
3030
<ScrollOverPack
3131
id="page1"
3232
className="page1" replay
33-
hideProps={{ 0: { reverse: true } }}
3433
>
3534
<TweenOne className="tween-one" key="0" animation={{ opacity: 1 }}>
36-
默认进入与出场
35+
默认进入与出场, 顶部出场
36+
</TweenOne>
37+
<QueueAnim key="1">
38+
<div key="0" className="demo"></div>
39+
<div key="1" className="demo" style={{ backgroundColor: '#F38EAD' }}></div>
40+
<div key="2" className="demo"></div>
41+
<div key="3" className="demo"></div>
42+
</QueueAnim>
43+
</ScrollOverPack>
44+
45+
<ScrollOverPack
46+
id="page4"
47+
className="page1"
48+
appear={false}
49+
>
50+
<TweenOne className="tween-one" key="0" animation={{ opacity: 1 }}>
51+
默认出场直接出现
3752
</TweenOne>
3853
<QueueAnim key="1">
3954
<div key="0" className="demo"></div>
@@ -48,7 +63,6 @@ class Demo extends React.Component {
4863
className="pack-page page2"
4964
style={{ backgroundColor: '#0098CE' }} always={false}
5065
id="page2"
51-
hideProps={{ title: { reverse: true } }}
5266
>
5367
<TweenOne key="title" animation={{ opacity: 0, type: 'from' }} className="page2-title">
5468
只进入一次
@@ -66,7 +80,6 @@ class Demo extends React.Component {
6680
className="pack-page page3"
6781
style={{ backgroundColor: '#174270' }}
6882
playScale={0.8} id="page3"
69-
hideProps={{ title: { reverse: true }, 1: { reverse: true } }}
7083
>
7184
<TweenOne
7285
animation={{ opacity: 1 }}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-scroll-anim",
3-
"version": "0.6.5",
3+
"version": "1.0.0",
44
"description": "scroll-anim anim component for react",
55
"keywords": [
66
"react",
@@ -58,7 +58,7 @@
5858
"dependencies": {
5959
"babel-runtime": "6.x",
6060
"raf": "3.x",
61-
"rc-tween-one": "~0.11.6",
61+
"rc-tween-one": "~1.1.2",
6262
"tween-functions": "1.x"
6363
}
6464
}

src/EventDispatcher.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ function EventDispatcher(target) {
33
this._listeners = {};
44
this._eventTarget = target || {};
55
this.recoverLists = [];
6+
this._listFun = {};
67
}
78
EventDispatcher.prototype = {
89
addEventListener(type, callback) {
@@ -26,12 +27,15 @@ EventDispatcher.prototype = {
2627
index = i + 1;
2728
}
2829
}
29-
const func = this.dispatchEvent.bind(this, _type);
30-
list.splice(index, 0, { c: callback, n: namespaces, t: _type, func });
31-
if (this._eventTarget.addEventListener) {
32-
this._eventTarget.addEventListener(_type, func, false);
33-
} else if (this._eventTarget.attachEvent) {
34-
this._eventTarget.attachEvent(`on${_type}`, func);
30+
31+
list.splice(index, 0, { c: callback, n: namespaces, t: _type });
32+
if (!this._listFun[_type]) {
33+
this._listFun[_type] = this._listFun[_type] || this.dispatchEvent.bind(this, _type);
34+
if (this._eventTarget.addEventListener) {
35+
this._eventTarget.addEventListener(_type, this._listFun[_type], false);
36+
} else if (this._eventTarget.attachEvent) {
37+
this._eventTarget.attachEvent(`on${_type}`, this._listFun[_type]);
38+
}
3539
}
3640
},
3741

@@ -49,12 +53,17 @@ EventDispatcher.prototype = {
4953
i = list.length;
5054
while (--i > -1) {
5155
if (list[i].c === callback && (_force || list[i].n === namespaces)) {
52-
if (this._eventTarget.removeEventListener) {
53-
this._eventTarget.removeEventListener(list[i].t, list[i].func);
54-
} else if (this._eventTarget.detachEvent) {
55-
this._eventTarget.detachEvent(`on${list[i].t}`, list[i].func);
56-
}
5756
list.splice(i, 1);
57+
if (!list.length) {
58+
const func = this._listFun[_type];
59+
delete this._listeners[_type];
60+
delete this._listFun[_type];
61+
if (this._eventTarget.removeEventListener) {
62+
this._eventTarget.removeEventListener(_type, func);
63+
} else if (this._eventTarget.detachEvent) {
64+
this._eventTarget.detachEvent(`on${_type}`, func);
65+
}
66+
}
5867
if (!_force) {
5968
return;
6069
}

src/ScrollElement.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ class ScrollElement extends React.Component {
1616
mapped.register(this.props.id, this.dom);
1717
}
1818
const date = Date.now();
19+
const scrollTop = currentScrollTop();
20+
if (!scrollTop) {
21+
this.scrollEventListener();
22+
}
1923
const length = EventListener._listeners.scroll ? EventListener._listeners.scroll.length : 0;
2024
this.eventType = `scroll.scrollEvent${date}${length}`;
21-
this.scrollEventListener();
2225
EventListener.addEventListener(this.eventType, this.scrollEventListener);
2326
}
2427

src/ScrollOverPack.jsx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { createElement } from 'react';
2+
import TweenOne from 'rc-tween-one';
23
import EventListener from './EventDispatcher';
34
import ScrollElement from './ScrollElement';
45
import { toArrayChildren } from './util';
@@ -9,18 +10,19 @@ function noop() {
910
class ScrollOverPack extends ScrollElement {
1011
constructor(props) {
1112
super(props);
12-
this.children = toArrayChildren(this.props.children);
13+
this.children = toArrayChildren(props.children);
1314
this.oneEnter = false;
1415
this.enter = false;
1516
this.state = {
1617
show: false,
17-
children: toArrayChildren(this.props.children),
18+
children: toArrayChildren(props.children),
1819
};
1920
}
2021

2122
scrollEventListener = (e) => {
2223
this.getParam(e);
23-
if (this.enter) {
24+
const isTop = this.elementShowHeight > this.clientHeight + this.leavePlayHeight;
25+
if (this.enter || !this.props.replay && isTop) {
2426
if (!this.state.show) {
2527
this.setState({
2628
show: true,
@@ -29,16 +31,16 @@ class ScrollOverPack extends ScrollElement {
2931
if (!this.props.always) {
3032
EventListener.removeEventListener(this.eventType, this.scrollEventListener);
3133
}
32-
}
33-
const bottomLeave = this.elementShowHeight < this.playHeight;
34-
// 设置往上时的出场点...
35-
const topLeave = this.props.replay ?
36-
this.elementShowHeight > this.clientHeight + this.leavePlayHeight : null;
37-
if (topLeave || bottomLeave) {
38-
if (this.state.show) {
39-
this.setState({
40-
show: false,
41-
});
34+
} else {
35+
const bottomLeave = this.elementShowHeight < this.playHeight;
36+
// 设置往上时的出场点...
37+
const topLeave = this.props.replay ? isTop : null;
38+
if (topLeave || bottomLeave) {
39+
if (this.state.show) {
40+
this.setState({
41+
show: false,
42+
});
43+
}
4244
}
4345
}
4446
}
@@ -51,12 +53,13 @@ class ScrollOverPack extends ScrollElement {
5153
'component',
5254
'always',
5355
'scrollEvent',
54-
'hideProps',
56+
'appear',
5557
'location',
5658
].forEach(key => delete placeholderProps[key]);
5759
let childToRender;
58-
if (!this.oneEnter && !this.state.show) {
59-
childToRender = createElement(this.props.component, { ...placeholderProps }, null);
60+
if (!this.oneEnter) {
61+
const children = !this.props.appear && this.props.children;
62+
childToRender = createElement(this.props.component, { ...placeholderProps }, children);
6063
this.oneEnter = true;
6164
} else {
6265
if (!this.state.show) {
@@ -65,9 +68,8 @@ class ScrollOverPack extends ScrollElement {
6568
return null;
6669
}
6770
let element;
68-
const hideProps = this.props.hideProps[item.key];
69-
if (hideProps) {
70-
element = React.cloneElement(item, { ...hideProps });
71+
if (item.type === TweenOne) {
72+
element = React.cloneElement(item, { reverse: true });
7173
return element;
7274
}
7375
element = React.cloneElement(item, {}, null);
@@ -91,7 +93,7 @@ ScrollOverPack.propTypes = {
9193
style: React.PropTypes.any,
9294
replay: React.PropTypes.bool,
9395
onChange: React.PropTypes.func,
94-
hideProps: React.PropTypes.object,
96+
appear: React.PropTypes.bool,
9597
};
9698

9799
ScrollOverPack.defaultProps = {
@@ -101,7 +103,7 @@ ScrollOverPack.defaultProps = {
101103
scrollEvent: noop,
102104
replay: false,
103105
onChange: noop,
104-
hideProps: {},
106+
appear: true,
105107
};
106108

107109
export default ScrollOverPack;

0 commit comments

Comments
 (0)