简体   繁体   English

有没有办法使用反应将 html 元素从 div 移动到另一个 div?

[英]Is there a way to move a html element from a div to another div using react?

how to move a HTML element from a div to another div element in React.如何将 HTML 元素从 div 移动到 React 中的另一个 div 元素。

I want an animation when an element moves from one div to another.当元素从一个 div 移动到另一个 div 时,我想要一个 animation。 If it can be done please suggest steps.如果可以,请提出步骤。

My suggestion would be我的建议是

  • Create a boolean in your react state for which div the element is supposed to be in.在您的反应 state 中创建一个 boolean 元素应该在哪个 div 中。
  • Make two functions, one that returns the element if the boolean is true and an empty div if it's false.创建两个函数,一个在 boolean 为真时返回元素,如果为假则返回空 div。 Then make a function that does the opposite.然后做一个相反的 function。
  • Drop them into their appropriate divs.将它们放入适当的 div 中。
  • Make a function attached to an event that swaps the boolean (whether it's a button, or a hover or whatever)将 function 附加到交换 boolean 的事件(无论是按钮,还是 hover 或其他)

If you want to get fancy and want an actual animation of the element moving, I'd suggest doing something like this:如果您想花哨并想要一个实际的 animation 元素移动,我建议您这样做:

#element{
animation: move 10s;
    -moz-animation: move 10s;
    /* Firefox */
    -webkit-animation: move 10s;
    /* Safari and Chrome */
    -o-animation: move 10s;
    /* Opera */

}

@keyframes move {
    0%{
      position: absolute;
      bottom: 20px;
     }
    50%{
      position: absolute;
      bottom: 80px;
    }

    100%{
        position: absolute;
        bottom: 140px;
    }

@-moz-keyframes move {
    /* Firefox */
     0%{
      position: absolute;
      bottom: 20px;
     }
    50%{
      position: absolute;
      bottom: 80px;
    }

    100%{
        position: absolute;
        bottom: 140px;
    }
}
@-webkit-keyframes move {
    /* Safari and Chrome */
     0%{
      position: absolute;
      bottom: 20px;
     }
    50%{
      position: absolute;
      bottom: 80px;
    }

    100%{
        position: absolute;
        bottom: 140px;
    }
}
@-o-keyframes move {
    /* Opera */
     0%{
      position: absolute;
      bottom: 20px;
     }
    50%{
      position: absolute;
      bottom: 80px;
    }

    100%{
        position: absolute;
        bottom: 140px;
    }
}

You might need to play around with the positioning and timing, but something like this can make an element start moving as soon as it's created in React.您可能需要调整定位和时间,但是这样的事情可以使元素在 React 中创建后立即开始移动。

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

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