@@ -7,8 +7,7 @@ import { objectEqual } from './util';
7
7
import Tween from './Tween' ;
8
8
import ticker from './ticker' ;
9
9
10
- function noop ( ) {
11
- }
10
+ function noop ( ) { }
12
11
13
12
const perFrame = Math . round ( 1000 / 60 ) ;
14
13
const objectOrArray = PropTypes . oneOfType ( [ PropTypes . object , PropTypes . array ] ) ;
@@ -65,7 +64,6 @@ class TweenOne extends Component {
65
64
// 跳帧事件 moment;
66
65
const nextMoment = props . moment ;
67
66
if ( typeof nextMoment === 'number' && nextMoment !== prevProps . moment ) {
68
-
69
67
if ( $self . tween && ! $self . updateAnim ) {
70
68
$self . startMoment = nextMoment ;
71
69
$self . startTime = ticker . time ;
@@ -105,16 +103,19 @@ class TweenOne extends Component {
105
103
if ( ! styleEqual ) {
106
104
// 在动画时更改了 style, 作为更改开始数值。
107
105
if ( $self . tween ) {
108
- $self . tween . reStart ( props . style , prevProps . style ,
109
- $self . tween . progressTime < $self . tween . totalTime ) ;
106
+ $self . tween . reStart (
107
+ props . style ,
108
+ prevProps . style ,
109
+ $self . tween . progressTime < $self . tween . totalTime ,
110
+ ) ;
110
111
if ( $self . paused ) {
111
112
$self . raf ( ) ;
112
113
}
113
114
}
114
115
}
115
116
$self . setForcedJudg ( props ) ;
116
117
}
117
- return nextState ; // eslint-disable-line
118
+ return nextState ; // eslint-disable-line
118
119
}
119
120
120
121
constructor ( props ) {
@@ -132,12 +133,14 @@ class TweenOne extends Component {
132
133
}
133
134
134
135
componentDidMount ( ) {
136
+ this . setDom ( )
135
137
if ( this . dom && this . dom . nodeName !== '#text' ) {
136
138
this . start ( ) ;
137
139
}
138
140
}
139
141
140
142
componentDidUpdate ( ) {
143
+ this . setDom ( ) ;
141
144
// 样式更新了后再执行动画;
142
145
if ( this . updateAnim && this . dom && this . dom . nodeName !== '#text' ) {
143
146
if ( this . tween ) {
@@ -166,7 +169,7 @@ class TweenOne extends Component {
166
169
* 暂时方案: 在组件里添加判断用的值。
167
170
*/
168
171
169
- setForcedJudg = ( props ) => {
172
+ setForcedJudg = props => {
170
173
Object . keys ( this . forced ) . forEach ( key => {
171
174
delete this [ key ] ;
172
175
delete this . forced [ key ] ;
@@ -179,12 +182,16 @@ class TweenOne extends Component {
179
182
}
180
183
} ) ;
181
184
}
182
- }
185
+ } ;
183
186
184
- setDefault = ( props ) => {
187
+ setDefault = props => {
185
188
this . moment = props . moment || 0 ;
186
189
this . startMoment = props . moment || 0 ;
187
190
this . startTime = ticker . time ;
191
+ } ;
192
+
193
+ setDom = ( ) => {
194
+ this . dom = this . dom || ReactDom . findDOMNode ( this ) ;
188
195
}
189
196
190
197
restart = ( ) => {
@@ -197,15 +204,14 @@ class TweenOne extends Component {
197
204
this . tween . reverseStartTime = this . startMoment ;
198
205
this . raf ( ) ;
199
206
this . play ( ) ;
200
- }
207
+ } ;
201
208
202
209
start = ( ) => {
203
210
this . updateAnim = false ;
204
211
const props = this . props ;
205
212
if ( props . animation && Object . keys ( props . animation ) . length ) {
206
213
this . setDefault ( props ) ;
207
- this . tween = new Tween ( this . dom , props . animation ,
208
- props . attr ) ;
214
+ this . tween = new Tween ( this . dom , props . animation , props . attr ) ;
209
215
this . tween . reverse = this . reverse ;
210
216
// 预先注册 raf, 初始动画数值。
211
217
this . raf ( ) ;
@@ -214,15 +220,15 @@ class TweenOne extends Component {
214
220
} else {
215
221
this . tween = null ;
216
222
}
217
- }
223
+ } ;
218
224
219
225
play = ( ) => {
220
226
this . cancelRequestAnimationFrame ( ) ;
221
227
if ( this . paused ) {
222
228
return ;
223
229
}
224
230
this . rafID = ticker . add ( this . raf ) ;
225
- }
231
+ } ;
226
232
227
233
frame = ( ) => {
228
234
const { yoyo } = this . props ;
@@ -238,14 +244,15 @@ class TweenOne extends Component {
238
244
let repeatNum = Math . floor ( moment / this . tween . totalTime ) || 0 ;
239
245
repeatNum = repeatNum > repeat ? repeat : repeatNum ;
240
246
let tweenMoment = moment - this . tween . totalTime * repeatNum ;
241
- tweenMoment = tweenMoment < perFrame && ! this . reverse &&
242
- totalTime >= perFrame ? 0 : tweenMoment ;
247
+ tweenMoment =
248
+ tweenMoment < perFrame && ! this . reverse && totalTime >= perFrame ? 0 : tweenMoment ;
243
249
if ( repeat && moment && moment - this . tween . totalTime * repeatNum < perFrame ) {
244
250
// 在重置样式之前补 complete;
245
251
this . tween . frame ( this . tween . totalTime * repeatNum ) ;
246
252
}
247
- if ( moment < this . moment && ! this . reverse ||
248
- repeat !== 0 && repeatNum && repeatNum !== this . repeatNum
253
+ if (
254
+ ( moment < this . moment && ! this . reverse ) ||
255
+ ( repeat !== 0 && repeatNum && repeatNum !== this . repeatNum )
249
256
) {
250
257
// 在 form 状态下,暂停时拉 moment 时,start 有值,,往返方向播放时,在 delay 的时间没有处理。。
251
258
// 与上面的处理一样,删除 start ,重新走一遍 start。。
@@ -256,22 +263,18 @@ class TweenOne extends Component {
256
263
if ( yoyoReverse ) {
257
264
tweenMoment = this . tween . totalTime - tweenMoment ;
258
265
}
259
- this . tween . onChange = ( e ) => {
266
+ this . tween . onChange = e => {
260
267
const cb = {
261
268
...e ,
262
269
timelineMode : '' ,
263
270
} ;
264
271
265
272
if (
266
- ( this . moment === this . startMoment &&
267
- ( ! this . reverse && ! e . index && e . mode === 'onStart' ) ||
268
- this . reverse )
273
+ ( this . moment === this . startMoment && ! this . reverse && ! e . index && e . mode === 'onStart' ) ||
274
+ this . reverse
269
275
) {
270
276
cb . timelineMode = 'onTimelineStart' ;
271
- } else if (
272
- moment >= totalTime && ! this . reverse ||
273
- ! moment && this . reverse
274
- ) {
277
+ } else if ( ( moment >= totalTime && ! this . reverse ) || ( ! moment && this . reverse ) ) {
275
278
cb . timelineMode = 'onTimelineComplete' ;
276
279
} else if ( repeatNum !== this . timelineRepeatNum ) {
277
280
cb . timelineMode = 'onTimelineRepeat' ;
@@ -284,7 +287,7 @@ class TweenOne extends Component {
284
287
this . moment = moment ;
285
288
this . repeatNum = repeatNum ;
286
289
this . tween . frame ( tweenMoment ) ;
287
- }
290
+ } ;
288
291
289
292
raf = ( ) => {
290
293
const tween = this . tween ;
@@ -295,18 +298,20 @@ class TweenOne extends Component {
295
298
}
296
299
const { repeat } = this . props ;
297
300
const totalTime = repeat === - 1 ? Number . MAX_VALUE : this . tween . totalTime * ( repeat + 1 ) ;
298
- if ( ( this . moment >= totalTime && ! this . reverse )
299
- || this . paused || ( this . reverse && this . moment === 0 )
301
+ if (
302
+ ( this . moment >= totalTime && ! this . reverse ) ||
303
+ this . paused ||
304
+ ( this . reverse && this . moment === 0 )
300
305
) {
301
306
return this . cancelRequestAnimationFrame ( ) ;
302
307
}
303
308
return null ;
304
- }
309
+ } ;
305
310
306
311
cancelRequestAnimationFrame = ( ) => {
307
312
ticker . clear ( this . rafID ) ;
308
313
this . rafID = - 1 ;
309
- }
314
+ } ;
310
315
311
316
render ( ) {
312
317
const {
@@ -333,8 +338,8 @@ class TweenOne extends Component {
333
338
} ) ;
334
339
// component 为空时调用子级的。。
335
340
const { className, children } = props ;
336
- const ref = ( c ) => {
337
- this . dom = c && c . props ? ReactDom . findDOMNode ( c ) : c ;
341
+ const ref = c => {
342
+ this . dom = c ;
338
343
} ;
339
344
if ( ! component && typeof children !== 'string' ) {
340
345
if ( ! children ) {
@@ -345,16 +350,16 @@ class TweenOne extends Component {
345
350
// 合并 style 与 className。
346
351
const newStyle = { ...childStyle , ...props . style } ;
347
352
const newClassName = className ? `${ className } ${ childClass } ` : childClass ;
348
- return React . cloneElement ( children , {
353
+ return React . cloneElement ( children , {
349
354
style : newStyle ,
350
355
ref,
351
356
className : newClassName ,
352
357
} ) ;
353
358
}
354
359
return React . createElement ( component , {
355
- ref,
360
+ ref : typeof component === 'string' ? ref : undefined ,
356
361
...props ,
357
- ...componentProps
362
+ ...componentProps ,
358
363
} ) ;
359
364
}
360
365
}
0 commit comments