Skip to content

Commit 6e7f97a

Browse files
committed
fix ant-design/ant-motion#262, height change active
1 parent a91c111 commit 6e7f97a

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

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": "2.7.3",
3+
"version": "2.7.4",
44
"description": "scroll-anim anim component for react",
55
"keywords": [
66
"react",

src/ScrollLink.jsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,11 @@ class ScrollLink extends React.Component {
7676
onClick = (e) => {
7777
e.preventDefault();
7878
EventListener.removeAllType('scroll.scrollAnchorEvent');
79-
const { elementDom, elementRect } = this.getElement();
80-
if (this.rafID !== -1 || !elementDom) {
79+
this.elementDom = document.getElementById(this.props.to);;
80+
if (this.rafID !== -1 || !this.elementDom) {
8181
return;
8282
}
8383
this.scrollTop = this.target ? this.target.scrollTop : currentScrollTop();
84-
const targetTop = this.target ? this.target.getBoundingClientRect().top : 0;
85-
const toTop = Math.round(elementRect.top + this.scrollTop) - this.props.offsetTop - targetTop;
86-
const t = transformArguments(this.props.showHeightActive)[0];
87-
const toShow = t.match('%') ? this.clientHeight * parseFloat(t) / 100 : t;
88-
this.toTop = this.props.toShowHeight ?
89-
toTop - toShow + 0.5 : toTop;
9084
this.initTime = Date.now();
9185
this.rafID = requestAnimationFrame(this.raf);
9286
scrollLinkLists.forEach(item => {
@@ -97,11 +91,15 @@ class ScrollLink extends React.Component {
9791
this.addActive();
9892
}
9993

100-
getElement = () => {
94+
getToTop = () => {
95+
const elementRect = this.elementDom && this.elementDom.getBoundingClientRect();
10196
this.clientHeight = this.target ? this.target.clientHeight : windowHeight();
102-
const elementDom = document.getElementById(this.props.to);
103-
const elementRect = elementDom && elementDom.getBoundingClientRect();
104-
return { elementDom, elementRect };
97+
const targetTop = this.target ? this.target.getBoundingClientRect().top : 0;
98+
const toTop = Math.round(elementRect.top + currentScrollTop()) - this.props.offsetTop - targetTop;
99+
const t = transformArguments(this.props.showHeightActive)[0];
100+
const toShow = t.match('%') ? this.clientHeight * parseFloat(t) / 100 : t;
101+
return this.props.toShowHeight ?
102+
toTop - toShow + 0.5 : toTop;
105103
}
106104

107105
cancelRequestAnimationFrame = () => {
@@ -127,21 +125,24 @@ class ScrollLink extends React.Component {
127125
}
128126
};
129127

128+
130129
raf = () => {
131130
if (this.rafID === -1) {
132131
return;
133132
}
134133
const duration = this.props.duration;
135134
const now = Date.now();
136135
const progressTime = now - this.initTime > duration ? duration : now - this.initTime;
136+
// 动画时也会改变高度,动态获取
137137
const easeValue = easingTypes[this.props.ease](progressTime, this.scrollTop,
138-
this.toTop, duration);
138+
this.getToTop(), duration);
139139
if (this.target) {
140140
this.target.scrollTop = easeValue;
141141
} else {
142142
window.scrollTo(window.scrollX, easeValue);
143143
}
144144
if (progressTime === duration) {
145+
this.elementDom = null;
145146
this.cancelRequestAnimationFrame();
146147
EventListener.reAllType('scroll.scrollAnchorEvent');
147148
} else {
@@ -163,19 +164,22 @@ class ScrollLink extends React.Component {
163164
}
164165

165166
scrollEventListener = () => {
166-
const { elementDom, elementRect } = this.getElement();
167+
const elementDom = document.getElementById(this.props.to);
167168
if (!elementDom) {
168169
return;
169170
}
171+
// 滚动时会改变高度, 动态获取高度
172+
const clientHeight = this.target ? this.target.clientHeight : windowHeight();
173+
const elementRect = elementDom.getBoundingClientRect();
170174
const elementClientHeight = elementDom.clientHeight;
171175
const targetTop = this.target ? this.target.getBoundingClientRect().top : 0;
172176
const top = Math.round(- elementRect.top + targetTop);
173177
const showHeightActive = transformArguments(this.props.showHeightActive);
174178
const startShowHeight = showHeightActive[0].toString().indexOf('%') >= 0 ?
175-
parseFloat(showHeightActive[0]) / 100 * this.clientHeight :
179+
parseFloat(showHeightActive[0]) / 100 * clientHeight :
176180
parseFloat(showHeightActive[0]);
177181
const endShowHeight = showHeightActive[1].toString().indexOf('%') >= 0 ?
178-
parseFloat(showHeightActive[1]) / 100 * this.clientHeight :
182+
parseFloat(showHeightActive[1]) / 100 * clientHeight :
179183
parseFloat(showHeightActive[1]);
180184
if (top >= Math.round(-startShowHeight)
181185
&& top < Math.round(elementClientHeight - endShowHeight)) {
@@ -186,7 +190,6 @@ class ScrollLink extends React.Component {
186190
}
187191

188192
render() {
189-
190193
const {
191194
component,
192195
onClick,

0 commit comments

Comments
 (0)