gpt4 book ai didi

javascript - React 组件的基于状态的动画

转载 作者:太空宇宙 更新时间:2023-11-04 08:18:16 25 4
gpt4 key购买 nike

我有一个组件 PromptBox,我想在状态从中心发生变化时在屏幕左侧设置动画。然后在另一个状态变化中,从右侧开始动画,进入中心

这是我的组件代码。我试图使用 this.isActive() 函数来管理状态和动画,这只会让组件消失。CSS:

.prompt-box {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #1E1E1E;
width: 35%;

-webkit-transition-property: left;
-webkit-transition-duration: 1.0;
-webkit-transition-property: ease-in-out;
}

// before animating in (off to the right)
.prompt-box.ready {
left: 100%;
}

// animating out to the left
.prompt-box.done {
left: -100%;
}

JS:

isActive() {
const { activity } = this.state;

if (activity === 'done') {
return 'prompt-box done';
} else if (activity === 'ready') {
return 'prompt-box ready';
} else {
return 'prompt-box';
}
}

render() {
return (
<div className={this.isActive()}>
<p className='title'>What's in your future?</p>

<ul className='options-holder'>
{
this.state.items.map((item, index) => (
<li key={item.id} className={`option ${index === 0 ? 'first' : ''}`}>
<div className='circle' onClick={this.removeItem} />

<p className='item-text'>{ item.text }</p>
</li>
))
}

<li key={0} className={`option form ${this.state.items.length === 0 ? 'only' : ''}`} ref={el => (this.formLi = el)}>
<div className='circle form' />

<form className='new-item-form' onSubmit={this.handleSubmit}>
<input
autoFocus
className='new-item-input'
placeholder='Type something and press return...'
onChange={this.handleChange}
value={this.state.text}
ref={input => (this.formInput = input)} />
</form>
</li>
</ul>

<button className='confirm-button' onClick={this.submitAndContinue}>Continue</button>
</div>
);
}

最佳答案

因此将您的 css 更新为:

...
-webkit-transition-property: left;
-webkit-transition-duration: 1s;
-webkit-transition-timing-function: ease-in-out;

关于javascript - React 组件的基于状态的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45744940/

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