Skip to content

Commit cc1ff36

Browse files
authored
Merge pull request #47 from react-component/child-context-style
Child context style
2 parents c906371 + 7aeafd9 commit cc1ff36

File tree

7 files changed

+170
-64
lines changed

7 files changed

+170
-64
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Tween from 'rc-tween-one';
2+
import React from 'react';
3+
import ReactDom from 'react-dom';
4+
import ScrollOverPack from 'rc-scroll-anim/lib/ScrollOverPack';
5+
import { Row, Col } from 'antd';
6+
7+
function Demo() {
8+
return (
9+
<div>
10+
<div style={{ height: 800 }}>往下滚动</div>
11+
<ScrollOverPack
12+
style={{ height: 800 }}
13+
component={Row}
14+
componentProps={{ gutter: { md: 12, sm: 5 } }}
15+
>
16+
<Tween
17+
key="1"
18+
animation={{ y: 30, type: 'from', ease: 'easeOutQuart', opacity: 0 }}
19+
reverseDelay={200}
20+
style={{ background: '#fff000' }}
21+
component={Col}
22+
>
23+
执行动画
24+
</Tween>
25+
</ScrollOverPack>
26+
</div>);
27+
}
28+
29+
ReactDom.render(<Demo />, document.getElementById('__react-content'));

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-tween-one",
3-
"version": "2.2.22",
3+
"version": "2.3.0",
44
"description": "tween-one anim component for react",
55
"keywords": [
66
"react",
@@ -67,15 +67,16 @@
6767
},
6868
"devDependencies": {
6969
"@types/react": "^16.0.0",
70+
"antd": "^3.12.1",
7071
"core-js": "^2.5.7",
7172
"expect.js": "0.3.x",
7273
"precommit-hook": "^3.0.0",
74+
"rc-queue-anim": "^1.6.1",
75+
"rc-scroll-anim": "2.x",
7376
"rc-test": "6.x",
7477
"rc-tools": "8.x",
7578
"react": "^16.4.0",
7679
"react-dom": "^16.4.0",
77-
"rc-queue-anim": "^1.6.1",
78-
"rc-scroll-anim": "2.x",
7980
"tslint-config-prettier": "^1.17.0",
8081
"tslint-react": "^3.6.0",
8182
"typescript": "3.x"

src/Tween.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
toFixed,
99
stylesToCss,
1010
createMatrix,
11+
getGsapType,
12+
isTransform,
1113
checkStyleName,
14+
toCssLowerCase,
1215
} from 'style-utils';
1316

1417
import easingTypes from './easing';
@@ -88,7 +91,7 @@ p.setAttrIsStyle = function () {
8891
}
8992
});
9093
data[i].style = _d;
91-
this.startDefaultData.style = this.target.getAttribute('style');
94+
this.startDefaultData.style = this.target.getAttribute('style') || '';
9295
} else if (this.attr === 'attr') {
9396
Object.keys(_d).forEach(key => {
9497
if (key === 'style' && Array.isArray(d[key])) {
@@ -97,7 +100,7 @@ p.setAttrIsStyle = function () {
97100
if (key === 'bezier') {
98101
_d.style = { ..._d.style, bezier: _d[key] };
99102
delete _d[key];
100-
this.startDefaultData.style = this.target.getAttribute('style');
103+
this.startDefaultData.style = this.target.getAttribute('style') || '';
101104
} else {
102105
this.startDefaultData[key] = this.target.getAttribute(key);
103106
}
@@ -391,24 +394,67 @@ p.resetAnimData = function () {
391394
this.start = {};
392395
};
393396

397+
const getDefaultStyle = function (domStyle, defaultStyle, tweenData) {
398+
const $data = defaultData({}, 0);
399+
const getStyleToArray = (styleString) => (
400+
styleString.split(';').filter(c => c).map(str =>
401+
str.split(':').map(s => s.trim())
402+
)
403+
);
404+
const styleToArray = getStyleToArray(defaultStyle);
405+
let domStyleToArray = getStyleToArray(domStyle);
406+
tweenData.forEach(value => {
407+
Object.keys(value).forEach(name => {
408+
if (!(name in $data)) {
409+
const styleName = toCssLowerCase(isTransform(getGsapType(name)));
410+
domStyleToArray = domStyleToArray.filter(item => {
411+
if (item[0].match(/transform|filter/ig) && styleName.match(/transform|filter/ig)) {
412+
return false;
413+
}
414+
return item[0] !== styleName;
415+
});
416+
}
417+
})
418+
});
419+
styleToArray.forEach(item => {
420+
domStyleToArray = domStyleToArray.map($item => {
421+
if ($item[0] === item[0]) {
422+
return item;
423+
}
424+
return $item;
425+
});
426+
})
427+
return domStyleToArray.map(item => item.join(':')).join(';');
428+
}
429+
394430
p.resetDefaultStyle = function () {
395431
this.tween = {};
396432
this.defaultData = this.defaultData.map(item => {
397433
item.reset = true;
398434
delete item.mode;
399435
return item;
400436
});
437+
const data = defaultData({}, 0);
401438
Object.keys(this.startDefaultData).forEach(key => {
402-
if (!(key in defaultData({}, 0))) {
403-
this.target.setAttribute(key, this.startDefaultData[key]);
439+
if (!(key in data)) {
440+
if (key === 'style') {
441+
const value = getDefaultStyle(this.target.style.cssText,
442+
this.startDefaultData.style,
443+
this.data);
444+
this.target.setAttribute(key, value);
445+
} else {
446+
this.target.setAttribute(key, this.startDefaultData[key]);
447+
}
404448
this.computedStyle = null;
405449
}
406450
});
407451
};
408452

409453
p.reStart = function (style) {
410454
this.start = {};
411-
this.target.style.cssText = '';
455+
this.target.style.cssText = getDefaultStyle(this.target.style.cssText,
456+
this.startDefaultData.style,
457+
this.data);
412458
Object.keys(style || {}).forEach(key => {
413459
this.target.style[key] = stylesToCss(key, style[key]);
414460
});

src/TweenOne.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class TweenOne extends Component {
7979
if (typeof nextMoment === 'number' && nextMoment !== this.props.moment) {
8080
if (this.tween && !this.updateAnim) {
8181
this.startMoment = nextMoment;
82-
this.startFrame = ticker.frame;
82+
this.startTime = ticker.time;
8383
if (nextProps.paused) {
8484
this.raf();
8585
}
@@ -176,15 +176,15 @@ class TweenOne extends Component {
176176
setDefalut = (props) => {
177177
this.moment = props.moment || 0;
178178
this.startMoment = props.moment || 0;
179-
this.startFrame = ticker.frame;
179+
this.startTime = ticker.time;
180180
}
181181

182182
restart = () => {
183183
if (!this.tween) {
184184
return;
185185
}
186186
this.startMoment = this.moment;
187-
this.startFrame = ticker.frame;
187+
this.startTime = ticker.time;
188188
this.tween.reverse = this.reverse;
189189
this.tween.reverseStartTime = this.startMoment;
190190
this.raf();
@@ -221,9 +221,9 @@ class TweenOne extends Component {
221221
let { repeat } = this.props;
222222
const totalTime = repeat === -1 ? Number.MAX_VALUE : this.tween.totalTime * (repeat + 1);
223223
repeat = repeat >= 0 ? repeat : Number.MAX_VALUE;
224-
let moment = (ticker.frame - this.startFrame) * perFrame + this.startMoment;
224+
let moment = ticker.time - this.startTime + this.startMoment;
225225
if (this.reverse) {
226-
moment = (this.startMoment || 0) - (ticker.frame - this.startFrame) * perFrame;
226+
moment = (this.startMoment || 0) - (ticker.time - this.startTime);
227227
}
228228
moment = moment > totalTime ? totalTime : moment;
229229
moment = moment <= 0 ? 0 : moment;

src/ticker.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Ticker.prototype = {
1313
perFrame: Math.round(1000 / 60),
1414
elapsed: 0,
1515
lastUpdate: getTime(),
16+
startTime: getTime(),// 开始时间,不计算 react 渲染时间;
17+
nextTime: 0,
18+
time: 0,
1619
};
1720
const p = Ticker.prototype;
1821
p.add = function (fn) {
@@ -40,12 +43,18 @@ p.sleep = function () {
4043
const ticker = new Ticker;
4144
p.tick = function (a) {
4245
ticker.elapsed = getTime() - ticker.lastUpdate;
46+
// 离开当前时设值 300;大于 300 则为离开。
47+
if (ticker.elapsed > 300) {
48+
ticker.startTime += ticker.elapsed - ticker.perFrame;
49+
}
4350
ticker.lastUpdate += ticker.elapsed;
44-
if (!ticker.frame) {
51+
ticker.time = ticker.lastUpdate - ticker.startTime;
52+
const overlap = ticker.time - ticker.nextTime;
53+
if(overlap > 0 || !ticker.frame) {
4554
ticker.frame++;
46-
} else {
47-
ticker.frame += Math.round(ticker.elapsed / ticker.perFrame);
55+
ticker.nextTime += overlap;
4856
}
57+
// console.log(ticker.frame, ticker.nextTime, ticker.time)
4958
ticker.tickFnArray.forEach(func => func(a));
5059
// 如果 object 里没对象了,自动杀掉;
5160
if (!ticker.tickFnArray.length) {
@@ -60,9 +69,9 @@ p.timeout = function (fn, time) {
6069
return console.warn('not function');// eslint-disable-line
6170
}
6271
const timeoutID = `timeout${Date.now()}-${timeoutIdNumber}`;
63-
const startFrame = this.frame;
72+
const startTime = this.time;
6473
this.wake(timeoutID, () => {
65-
const moment = (this.frame - startFrame) * this.perFrame;
74+
const moment = this.time - startTime;
6675
if (moment >= (time || 0)) {
6776
this.clear(timeoutID);
6877
fn();
@@ -78,11 +87,11 @@ p.interval = function (fn, time) {
7887
return null;
7988
}
8089
const intervalID = `interval${Date.now()}-${intervalIdNumber}`;
81-
let starFrame = this.frame;
90+
let starTime = this.time;
8291
this.wake(intervalID, () => {
83-
const moment = (this.frame - starFrame) * this.perFrame;
92+
const moment = this.time - starTime;
8493
if (moment >= (time || 0)) {
85-
starFrame = this.frame;
94+
starTime = this.time;
8695
fn();
8796
}
8897
});

0 commit comments

Comments
 (0)