gpt4 book ai didi

javascript - react CSSTransition 不动画

转载 作者:行者123 更新时间:2023-11-29 10:55:31 27 4
gpt4 key购买 nike

我想在我的网络应用程序中为一个框设置动画(淡入、淡出)。我正在使用 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是工作代码。单击“打开模式”打开模式,然后单击“切换框”打开/关闭我想要设置动画的框。

编辑:我实际上是在尝试让框在切换时滑入和滑出。这里是

更新后的 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 的挂载/卸载。

  <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%;
}

关于javascript - react CSSTransition 不动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com