反应 CSSTransition 不动画
React CSSTransition not animating
我想在我的网络应用程序中为一个框设置动画(淡入、淡出)。我为此使用 react-transition-group。但不知何故,动画不起作用。代码
import React from 'react';
import ReactDOM from 'react-dom';
import { CSSTransition } from 'react-transition-group';
import Modal from 'react-modal';
import './styles.css';
class Example extends React.Component {
state = {
isOpen: false,
};
toggleModal = () => {
this.setState(prevState => ({
isOpen: !prevState.isOpen,
}));
};
render() {
const modalStyles = {
overlay: {
backgroundColor: '#ffffff',
},
};
return (
<div>
<button onClick={this.toggleModal}>
Open Modal
</button>
<CSSTransition
in={this.state.isOpen}
timeout={300}
classNames="dialog"
>
<Modal
isOpen={this.state.isOpen}
style={modalStyles}
>
<button onClick={this.toggleModal}>
Close Modal
</button>
<div>Hello World</div>
</Modal>
</CSSTransition>
</div>
);
}
}
CSS:
.dialog-enter {
opacity: 0;
transition: opacity 500ms linear;
}
.dialog-enter-active {
opacity: 1;
}
.dialog-exit {
opacity: 0;
}
.dialog-exit-active {
opacity: 1;
}
.box {
width: 200px;
height: 100vh;
background: blue;
color: white;
}
Here 是工作代码。单击 "Open Modal" 打开模式,然后单击 "Toggle Box" 到 open/close 我要设置动画的框。
编辑:我实际上是想在切换时让盒子滑入和滑出。这里是
更新后的CSS:
.dialog-enter {
left: 100%;
transition: left 1500ms;
}
.dialog-enter-active {
left: 0;
}
.dialog-exit {
left: 0;
transition: left 1500ms;
}
.dialog-exit-active {
left: 100%;
}
.box {
position: absolute;
width: 200px;
height: 50vh;
background: blue;
更新代码 link
您必须完全信任 CSSTransition 的 mount/unmount。
<CSSTransition
in={this.state.boxVisible}
timeout={1500}
classNames="dialog"
unmountOnExit
>
<div>
<div className="box">Box</div>
</div>
</CSSTransition>
看这里:https://codesandbox.io/s/csstransition-component-9obbb
更新:
如果您想按照评论中的要求移动左侧 css 属性 的元素。您还必须添加 position: realative 样式。
.dialog-enter {
left: 100%;
transition: left 1500ms;
position: relative;
}
.dialog-enter-active {
left: 0;
}
.dialog-exit {
left: 0;
transition: left 1500ms;
position: relative;
}
.dialog-exit-active {
left: 100%;
}
我想在我的网络应用程序中为一个框设置动画(淡入、淡出)。我为此使用 react-transition-group。但不知何故,动画不起作用。代码
import React from 'react';
import ReactDOM from 'react-dom';
import { CSSTransition } from 'react-transition-group';
import Modal from 'react-modal';
import './styles.css';
class Example extends React.Component {
state = {
isOpen: false,
};
toggleModal = () => {
this.setState(prevState => ({
isOpen: !prevState.isOpen,
}));
};
render() {
const modalStyles = {
overlay: {
backgroundColor: '#ffffff',
},
};
return (
<div>
<button onClick={this.toggleModal}>
Open Modal
</button>
<CSSTransition
in={this.state.isOpen}
timeout={300}
classNames="dialog"
>
<Modal
isOpen={this.state.isOpen}
style={modalStyles}
>
<button onClick={this.toggleModal}>
Close Modal
</button>
<div>Hello World</div>
</Modal>
</CSSTransition>
</div>
);
}
}
CSS:
.dialog-enter {
opacity: 0;
transition: opacity 500ms linear;
}
.dialog-enter-active {
opacity: 1;
}
.dialog-exit {
opacity: 0;
}
.dialog-exit-active {
opacity: 1;
}
.box {
width: 200px;
height: 100vh;
background: blue;
color: white;
}
Here 是工作代码。单击 "Open Modal" 打开模式,然后单击 "Toggle Box" 到 open/close 我要设置动画的框。
编辑:我实际上是想在切换时让盒子滑入和滑出。这里是
更新后的CSS:
.dialog-enter {
left: 100%;
transition: left 1500ms;
}
.dialog-enter-active {
left: 0;
}
.dialog-exit {
left: 0;
transition: left 1500ms;
}
.dialog-exit-active {
left: 100%;
}
.box {
position: absolute;
width: 200px;
height: 50vh;
background: blue;
更新代码 link
您必须完全信任 CSSTransition 的 mount/unmount。
<CSSTransition
in={this.state.boxVisible}
timeout={1500}
classNames="dialog"
unmountOnExit
>
<div>
<div className="box">Box</div>
</div>
</CSSTransition>
看这里:https://codesandbox.io/s/csstransition-component-9obbb
更新: 如果您想按照评论中的要求移动左侧 css 属性 的元素。您还必须添加 position: realative 样式。
.dialog-enter {
left: 100%;
transition: left 1500ms;
position: relative;
}
.dialog-enter-active {
left: 0;
}
.dialog-exit {
left: 0;
transition: left 1500ms;
position: relative;
}
.dialog-exit-active {
left: 100%;
}