在对react元素进行css转换时,需要处理一个附加状态:转换本身。
因此,首先更改组件的状态,以便CSS转换将更改它们,然后更改转换结束时的状态,使react与转换结束时组件的样式匹配。
在您的例子中,当您单击切换元素时,您必须计算一个与切换元素效果相同的CSS更改,然后当转换结束时,您实际切换元素。
我们需要向项添加一个Top属性和一个适当的键:
constructor() {
super();
this.state = {
items: [
{ key: 0, name: "Hello", top: 0 },
{ key: 1, name: "Sello", top: 0 }, // move this down
{ key: 2, name: "Wello", top: 0 }, // move this up
{ key: 3, name: "Zello", top: 0 },
{ key: 4, name: "Pello", top: 0 },
{ key: 5, name: "Aello", top: 0 }
],
transitioning: {}
};
this.itemTransitionCount = 0;
}
记下itemtrantincount以备以后使用。
首先包装你的
<li>
元素在
<ul>
元素并将引用附加到
<UL & GT;
:
ulLoad = c => (this.ulDom = c);
render() {
...
<ul
ref={this.ulLoad}
style={{ position: "relative", display: "inline-block" }}
>
{
this.state.items.map(item => (
<li
key={item.key}
onTransitionEnd={this.itemTransitionEnd}
style={{
transition: item.top ? "top 0.5s ease" : "none",
position: "relative",
top: item.top
}}
>
{item.name}
</li>
))
}
</ul>
....
}
这将允许计算子对象的相对位置
<理工大学;
S
然后以这种方式更改重组处理程序:
reShuffle = () => {
this.setState({
items: this.state.items.map((item, index) => {
if (index === 1) {
const top2 = this.ulDom.children[2].offsetTop;
const top1 = this.ulDom.children[1].offsetTop;
const top = top2 - top1;
return { ...this.state.items[1], top };
}
if (index === 2) {
const top1 = this.ulDom.children[1].offsetTop;
const top2 = this.ulDom.children[2].offsetTop;
const top = top1 - top2;
return { ...this.state.items[2], top };
}
return item;
})
});
此代码将启动要在li项上设置的顶部转换=>切换动画将在下一次渲染期间发生。
我们用onTransitionEnd处理程序(对evt.target的检查)捕获两个动画的结尾。==evt.currentTarget存在是因为转换事件气泡,计数存在是因为两个元素都将引发事件,但我们希望在两个转换结束时执行一次操作):
itemTransitionEnd = evt => {
if (evt.target !== evt.currentTarget) return;
// alert("transitionEnd");
this.itemTransitionCount++;
if (this.itemTransitionCount < 2) return;
this.itemTransitionCount = 0;
this.setState({
items: this.state.items.map((item, index) => {
if (index === 1) {
return { ...this.state.items[2], top: 0 };
}
if (index === 2) {
return { ...this.state.items[1], top: 0 };
}
return item;
})
});
};
上面的代码实际上会在转换结束时切换元素并重置其相对顶部(转换计数也会为下一个转换重置)。
这说明了在React中对CSS转换动画进行两步渲染。
Working sandbox here