简体   繁体   English

淡入一个组件并淡入另一个React.js

[英]Fading a component in and fading another one out React.js

I just recently started getting into using React.js and to make it easier for myself I'm trying to recreate projects I've made in the past, but instead of using jQuery like I did in the past I'm completely avoiding jQuery and using only React. 我最近才开始使用React.js,为了使自己更容易,我试图重新创建过去创建的项目,但是我没有像过去那样使用jQuery,而是完全避免了jQuery和仅使用React。

I tend to do animations where a div would fade in as another fades out like this: 我倾向于制作一个div会随着另一个淡入淡出的动画,如下所示:

$("#start").click(function() {
    $("#h1").fadeOut(750);
    $("#h2").delay(500).fadeIn(750);
    $("#h1").css("z-index", 0);
    $("#h2").css("z-index", 1);
}); 

I was wondering how can I reproduce this fade in and out effect without jQuery 我想知道如何在没有jQuery的情况下重现这种淡入和淡出效果

(I know CSS animations could change the opacity, but the opacity isn't the only thing I'm trying to change, this affects the display property as well). (我知道CSS动画可以更改不透明度,但是不透明度不是我要更改的唯一内容,这也会影响display属性)。

One option would be to use a framework, like react-bootstrap, which includes a lot of the UI components you need for any given project. 一种选择是使用诸如react-bootstrap之类的框架,其中包括许多给定项目所需的UI组件。 It includes a Fade component. 它包括一个Fade组件。 Documentation can be found here: https://react-bootstrap.github.io/components.html#utilities 可以在这里找到文档: https : //react-bootstrap.github.io/components.html#utilities

class Example extends React.Component {

 constructor(...args) {
  super(...args);
  this.state = {};
 }

 render() {
  return (
    <div>
      <Button onClick={()=> this.setState({ open: !this.state.open })}>
       click
      </Button>
      <Fade in={this.state.open}>
        <div>
          <Well>
            THIS CONTENT WILL FADE
          </Well>
        </div>
      </Fade>
    </div>
  );
 }
}

ReactDOM.render(<Example/>, mountNode);

A simple way is to use CSS transitions. 一种简单的方法是使用CSS过渡。 Basically you just add a class to an element, and define a transition in the CSS and it does the rest for you 基本上,您只需将一个类添加到元素,然后在CSS中定义一个过渡,其余的工作就为您完成

There is a tutorial here 这里有一个教程

https://www.w3schools.com/css/css3_transitions.asp https://www.w3schools.com/css/css3_transitions.asp

Which does a good job of explaining how it all works with examples and a playground for you to try your own 通过示例和游乐场,您可以很好地解释它们的全部工作原理,让您自己尝试

The CSS Transition group add-on might help, it let's you define transitions like this: CSS Transition组附加组件可能会有所帮助,让我们定义如下过渡:

JS: JS:

<ReactCSSTransitionGroup
  transitionName="example"
  transitionEnterTimeout={500}
  transitionLeaveTimeout={300}>
  {items}
</ReactCSSTransitionGroup>

CSS: CSS:

.example-enter {
  opacity: 0.01;
}

.example-enter.example-enter-active {
  opacity: 1;
  transition: opacity 500ms ease-in;
}

.example-leave {
  opacity: 1;
}

.example-leave.example-leave-active {
  opacity: 0.01;
  transition: opacity 300ms ease-in;
}

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

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