简体   繁体   English

如何在进入 dom 时为 React 组件设置动画?

[英]How do I animate React component upon entering the dom?

I have code something like this in react我在反应中有这样的代码

{this.state.popoverOpen && <Popover/>}

it's easy, but when the component actually appears I want it to come in with opacity changing and animating... I've been working with react for some time but these cases always leave room for confusion for me... So whats the best and easy solution?这很容易,但是当组件实际出现时,我希望它具有不透明度变化和动画效果......我一直在使用 react 一段时间,但这些情况总是给我留下困惑的空间......所以最好的和简单的解决方案? no applying classes work obviously at this point...在这一点上显然没有应用课程......

You can use CSS transitions.您可以使用 CSS 转换。 Try adding the fade-in to the className of Popover 's outermost HTML element after adding the code below to the relevant CSS file.在将以下代码添加到相关的 CSS 文件后,尝试将fade-in添加到Popover的最外层 HTML 元素的className中。

.fade-in {
  -webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadein 2s; /* Firefox < 16 */
  -ms-animation: fadein 2s; /* Internet Explorer */
  -o-animation: fadein 2s; /* Opera < 12.1 */
  animation: fadein 2s;
}

@keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
  from { opacity: 0; }
  to   { opacity: 1; }
}

Codepen example: https://codepen.io/rodenmonte/pen/LYpOVpb Codepen 示例: https://codepen.io/rodenmonte/pen/LYpOVpb

Here's a StackOverflow answer showing another (better IMO) way to do this ReactJS: Fade in div and fade out div based on state这是一个 StackOverflow 答案,显示了另一种(更好的 IMO)方法来执行此操作 ReactJS:基于 state 淡入 div 和淡出 div

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

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