反应动画以将元素从一个父对象移动到另一个父元素 [英] React animation for moving an element from one parent to another
问题描述
我正在尝试创建动画,以使用React将子元素从一个父元素移动到另一个父元素.
I am trying to create an animation for moving a child element from one parent element to another using React.
用户应该能够单击一个元素并将其移至另一个div.
A user should be able to click on an element and see it move into another div.
我制作了一个简单的演示组件(不带动画)以显示我的意思.单击某个元素后,状态将更新,并且将元素重新渲染到正确的位置.
I made a simple demo component (without the animation) to show what I mean. When an element is clicked, the state updates and the elements are re-rendered in the correct place.
class App extends React.Component {
state = {
list: ['Alice', 'Bob', 'Charlie', 'David', 'Emily', 'Frank'],
top: [0, 1, 2],
bottom: [3, 4, 5]
}
moveDown = (item) => {
let { top, bottom } = this.state
this.setState({
top: top.filter(x => x !== item),
bottom: [...bottom, item]
})
}
moveUp = (item) => {
let { top, bottom } = this.state
this.setState({
top: [...top, item],
bottom: bottom.filter(x => x !== item)
})
}
render() {
let { top, bottom, list } = this.state
return (
<div style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-between',
alignItems: 'center',
height: '90vh',
width: '100%'
}}>
<div>
{top.map((item) =>
<div
onClick={() => this.moveDown(item)}
style={{color:'red'}}>{list[item]}</div>
)}
</div>
<div>
{bottom.map((item) =>
<div
onClick={() => this.moveUp(item)}
style={{color:'green'}}>{list[item]}</div>
)}
</div>
</div>
)
}
}
Codepen演示: https://codepen.io/ee92/pen/LqrBjL?编辑者= 0010
Codepen demo: https://codepen.io/ee92/pen/LqrBjL?editors=0010
衷心感谢并感谢您对如何实现此div到div动画的任何帮助或建议.
Big appreciation to and thanks in advance for any help or advice on how to achieve this div-to-div animation.
推荐答案
不可能
无法以这种方式进行动画处理,因为DOM认为您正在删除div
然后添加新的div
.即使您使用相同的div
,DOM也没有该上下文.动画是通过更改CSS(而不是HTML)来控制的.
It's not possible to animate in that way because the DOM thinks you're removing a div
and then adding a new div
. Even though it's the same div
to you, the DOM doesn't have that context. Animations are controlled by changes to CSS, not HTML.
...但是这是操作方法
如果您实际上需要将两个列表都保留在不同的div
中,则您可以执行以下操作之一:
If you actually need both lists to stay in different div
s the best you can do is either:
- 将
old item
设置为new item
位置,然后删除old item
并显示new item
. - 删除
old item
并在old item
所在的位置创建一个new item
,然后将其移至new item
位置.
- Animate the
old item
to thenew item
position, then delete theold item
and show thenew item
. - Remove the
old item
and create anew item
where theold item
was and move it to thenew item
position.
相同的概念,两种实现方式.
Same concept, two ways of doing it.
我修改了您现有的样本,以显示选项2的简化版本.请注意,有许多动画决策可以做出来,例如列表变小时会发生什么,项目应如何从红色到绿色等,我没有尝试客观地解决它们.另外,如果您可以将两个列表的所有item
都放在一个div
中,并控制它们的位置absolute
ly,则将更加容易.但是,如果他们需要以单独的div
s ...
I modified your existing sample to show a simplified version of option 2. Note that there are a number of animation decisions to make like what happens when the list gets smaller, how should the items change from red to green, etc., and I didn't try and objectively solve them. Also, this would be much easier if you could have all the item
s for both lists in one div
, and control their positions absolute
ly. But if they need to end up in separate div
s...
https://codepen.io/sallf/pen/VgBwQr?editors=0010
发生了什么事
- 在
.item
中添加transition
可以在调整transform
属性时使动画发生. - 在项目单击时,我们会更新状态列表并添加...
-
transition.item
知道哪个项目正在制作动画... -
transition.startTop
知道项目应相对于要移动的列表底部的偏移量y
的位置,然后... -
transition.startAnim
作为控制动画的标志. - 由于
transition
动画前需要进行一些更改,因此我们使用setTimeout
延迟transition.startAnim
的更改,这基本上会使动画从计算的位置返回到0
.
- Adding a
transition
to.item
we can make the animation happen when we make adjustments to thetransform
property. - On item click we update our lists in state and add...
transition.item
to know which item is animating...transition.startTop
to know the offsety
position the item should start at relative to the bottom of the list it's moving to, and...transition.startAnim
as a flag to control the animation.- Since
transition
s need something to change before they'll animate, we usesetTimeout
to delay the change oftransition.startAnim
which basically causes the animation from the computed position, back to0
.
这篇关于反应动画以将元素从一个父对象移动到另一个父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!