@@ -76,17 +76,11 @@ class ScrollLink extends React.Component {
76
76
onClick = ( e ) => {
77
77
e . preventDefault ( ) ;
78
78
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 ) {
81
81
return ;
82
82
}
83
83
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 ;
90
84
this . initTime = Date . now ( ) ;
91
85
this . rafID = requestAnimationFrame ( this . raf ) ;
92
86
scrollLinkLists . forEach ( item => {
@@ -97,11 +91,15 @@ class ScrollLink extends React.Component {
97
91
this . addActive ( ) ;
98
92
}
99
93
100
- getElement = ( ) => {
94
+ getToTop = ( ) => {
95
+ const elementRect = this . elementDom && this . elementDom . getBoundingClientRect ( ) ;
101
96
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 ;
105
103
}
106
104
107
105
cancelRequestAnimationFrame = ( ) => {
@@ -127,21 +125,24 @@ class ScrollLink extends React.Component {
127
125
}
128
126
} ;
129
127
128
+
130
129
raf = ( ) => {
131
130
if ( this . rafID === - 1 ) {
132
131
return ;
133
132
}
134
133
const duration = this . props . duration ;
135
134
const now = Date . now ( ) ;
136
135
const progressTime = now - this . initTime > duration ? duration : now - this . initTime ;
136
+ // 动画时也会改变高度,动态获取
137
137
const easeValue = easingTypes [ this . props . ease ] ( progressTime , this . scrollTop ,
138
- this . toTop , duration ) ;
138
+ this . getToTop ( ) , duration ) ;
139
139
if ( this . target ) {
140
140
this . target . scrollTop = easeValue ;
141
141
} else {
142
142
window . scrollTo ( window . scrollX , easeValue ) ;
143
143
}
144
144
if ( progressTime === duration ) {
145
+ this . elementDom = null ;
145
146
this . cancelRequestAnimationFrame ( ) ;
146
147
EventListener . reAllType ( 'scroll.scrollAnchorEvent' ) ;
147
148
} else {
@@ -163,19 +164,22 @@ class ScrollLink extends React.Component {
163
164
}
164
165
165
166
scrollEventListener = ( ) => {
166
- const { elementDom, elementRect } = this . getElement ( ) ;
167
+ const elementDom = document . getElementById ( this . props . to ) ;
167
168
if ( ! elementDom ) {
168
169
return ;
169
170
}
171
+ // 滚动时会改变高度, 动态获取高度
172
+ const clientHeight = this . target ? this . target . clientHeight : windowHeight ( ) ;
173
+ const elementRect = elementDom . getBoundingClientRect ( ) ;
170
174
const elementClientHeight = elementDom . clientHeight ;
171
175
const targetTop = this . target ? this . target . getBoundingClientRect ( ) . top : 0 ;
172
176
const top = Math . round ( - elementRect . top + targetTop ) ;
173
177
const showHeightActive = transformArguments ( this . props . showHeightActive ) ;
174
178
const startShowHeight = showHeightActive [ 0 ] . toString ( ) . indexOf ( '%' ) >= 0 ?
175
- parseFloat ( showHeightActive [ 0 ] ) / 100 * this . clientHeight :
179
+ parseFloat ( showHeightActive [ 0 ] ) / 100 * clientHeight :
176
180
parseFloat ( showHeightActive [ 0 ] ) ;
177
181
const endShowHeight = showHeightActive [ 1 ] . toString ( ) . indexOf ( '%' ) >= 0 ?
178
- parseFloat ( showHeightActive [ 1 ] ) / 100 * this . clientHeight :
182
+ parseFloat ( showHeightActive [ 1 ] ) / 100 * clientHeight :
179
183
parseFloat ( showHeightActive [ 1 ] ) ;
180
184
if ( top >= Math . round ( - startShowHeight )
181
185
&& top < Math . round ( elementClientHeight - endShowHeight ) ) {
@@ -186,7 +190,6 @@ class ScrollLink extends React.Component {
186
190
}
187
191
188
192
render ( ) {
189
-
190
193
const {
191
194
component,
192
195
onClick,
0 commit comments