简体   繁体   English

动画div在React中的容器div之间移动-react-pose?

[英]Animating div to move between container divs in React - react-pose?

https://codesandbox.io/s/jvq5yk1n29 https://codesandbox.io/s/jvq5yk1n29

This little sandbox moves a dot back and forth between five material-ui Paper boxes when arrow keys are pressed. 这个小沙箱移动的点来回五岁的material-ui Paper当按下方向键框。 How would I create a smooth animation as it moves from box to box? 在框与框之间移动时如何创建平滑的动画? I came up with a very convoluted react-pose solution that constantly polls the getClientBoundingRect() on the container DOM elements in order to move the dot with absolute coordinates. 我想出了一个非常复杂的react-pose解决方案,该解决方案不断轮询容器DOM元素上的getClientBoundingRect() ,以便使用绝对坐标移动点。 But that's just a pain. 但这只是一个痛苦。 There must be something more elegant? 一定会有更优雅的东西吗? I can't use CSS (?) since the div is constantly being remounted, leaving nothing for an animation to hook onto. 我不能使用CSS(?),因为div一直在不断重装,因此没有任何动画可以挂在上面。 What gives? 是什么赋予了?

You can achieve that with a combination of JS and CSS . 您可以结合使用JSCSS来实现。

Here're the steps: 步骤如下:

  1. Make the dot element ready to move right or left(in CSS). 使dot元素准备向右或向左移动(在CSS中)。
  2. Create a state variable that keeps a track from which side (eg left or right ) the dot comes in. 创建一个状态变量,以跟踪点从哪一侧进入(例如, leftright )。
  3. Pass the state variable to your Dot component and change the style accordingly. 将状态变量传递给Dot组件,并相应地更改样式。

在移动中编辑点

This technic gives you really the flexibility on how you want to animate your element in React using the state . 这项技术为您提供了真正的灵活性,让您可以灵活地使用stateReact中对元素进行动画处理。

Bonus: after I read your code, I'd like to recommend you the following: 附:在阅读您的代码后,我向您推荐以下内容:

  • Try always to use === with your comparisons. 尝试始终在比较中使用===
  • Any element that's absolutely positioned, its parent should have relatively positioned (ie position: relative ) 任何绝对定位的元素,其父元素都应相对定位(即position: relative
  • Try not to duplicate your CSS rules. 尽量不要重复您的CSS规则。

Hope that helped! 希望能有所帮助! Let me know if it isn't clear! 让我知道是否不清楚!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM