React Web 动画的 5 种创建方式,每一种都不简单
以下便是本文要分享的创建
下面,勒次个特斯大特一特 CSS animation给元素添加
接下来,我们通过一个实例来体验一下这种创建方式: 首先,我们要创建两个 .input { width: 150px; padding: 10px; font-size: 20px; border: none; border-radius: 4px; background-color: #dddddd; transition: width .35s linear; outline: none; } .input-focused { width: 240px; } 一个是它 下面,我们就开始书写我们的 在此,推荐一个 在线的 React VS Code IDE,真的很强大,读者不想构建自己的
class App extends Component { state = { focused: false,} componentDidMount() { this._input.addEventListener('focus',this.focus); this._input.addEventListener('blur',this.focus); } focus = () => { this.setState(prevState => ({ focused: !prevState.focused,})); } render() { return ( <div className="App"> <div className="container"> <input ref={input => this._input = input} className={['input',this.state.focused && 'input-focused'].join(' ')} /> </div> </div> ); } }
JS Style
在下面的实例中,我们将创建一个
class App extends Component { state = { disabled: true,} onChange = (e) => { const length = e.target.value.length; if (length > 0) { this.setState({ disabled: false }); } else { this.setState({ disabled: true }); } } render() { const { disabled } = this.state; const label = disabled ? 'Disabled' : 'Submit'; return ( <div style={styles.App}> <input style={styles.input} onChange={this.onChange} /> <button style={Object.assign({},styles.button,!this.state.disabled && styles.buttonEnabled )} disabled={disabled} > {label} </button> </div> ); } } const styles = { App: { display: 'flex',justifyContent: 'left',},input: { marginRight: 10,padding: 10,width: 190,fontSize: 20,border: 'none',backgroundColor: '#ddd',outline: 'none',button: { width: 90,height: 43,fontSize: 17,borderRadius: 4,transition: '.25s all',cursor: 'pointer',buttonEnabled: { width: 120,backgroundColor: '#ffc107',} }
React Motion
对于绝大多数的动画组件,我们往往不希望对 下面是一个 <Motion style={{ x: spring(this.state.x) }}> { ({ x }) => <div style={{ transform: `translateX(${x}px)` }} /> } </Motion> 这是官方提供的几个
为了使用它,首先我们要用 yarn add react-motion 在下面的实例中,我们将创建一个
class App extends Component { state = { height: 38,} animate = () => { this.setState((state) => ({ height: state.height === 233 ? 38 : 233 })); } render() { return ( <div className="App"> <div style={styles.button} onClick={this.animate}>Animate</div> <Motion style={{ height: spring(this.state.height) }} > { ({ height }) => <div style={Object.assign({},styles.menu,{ height } )}> <p style={styles.selection}>Selection 1</p> <p style={styles.selection}>Selection 2</p> <p style={styles.selection}>Selection 3</p> <p style={styles.selection}>Selection 4</p> <p style={styles.selection}>Selection 5</p> <p style={styles.selection}>Selection 6</p> </div> } </Motion> </div> ); } } const styles = { menu: { marginTop: 20,width: 300,border: '2px solid #ddd',overflow: 'hidden',button: { display: 'flex',width: 200,height: 45,justifyContent: 'center',alignItems: 'center',selection: { margin: 0,borderBottom: '1px solid #ededed',}
Animated
它背后的思想是创建
为了使用 yarn add animated 在下面的实例中,我们将模拟在提交表单成功后显示的动画
import Animated from 'animated/lib/targets/react-dom'; import Easing from 'animated/lib/Easing'; class AnimatedApp extends Component { animatedValue = new Animated.Value(0); animate = () => { this.animatedValue.setValue(0); Animated.timing( this.animatedValue,{ toValue: 1,duration: 1000,easing: Easing.elastic(1),} ).start(); } render() { const marginLeft = this.animatedValue.interpolate({ inputRange: [0,1],outputRange: [-120,0],}); return ( <div className="App"> <div style={styles.button} onClick={this.animate}>Animate</div> <Animated.div style={ Object.assign( {},styles.box,{ opacity: this.animatedValue,marginLeft })} > <p>Thanks for your submission!</p> </Animated.div> </div>- 我们将 `animatedValue` 和 `marginLeft` 作为 `Animated.div ` 的 `style` 属性, ); } } const styles = { button: { display: 'flex',width: 125,height: 50,box: { display: 'inline-block',marginTop: 10,padding: '0.6rem 2rem',fontSize:'0.8rem',border: '1px #eee solid',boxShadow: '0 2px 8px rgba(0,.2)',}
Velocity React
下面是一个 <VelocityComponent animation={{ opacity: this.state.showSubComponent ? 1 : 0 }} duration={500} > <MySubComponent/> </VelocityComponent> 首先还是要用 yarn add velocity-react 在下面的实例中,我们将创建一个很酷的
import { VelocityComponent } from 'velocity-react'; const VelocityLetter = ({ letter }) => ( <VelocityComponent runOnMount animation={{ opacity: 1,marginTop: 0 }} duration={500} > <p style={styles.letter}>{letter}</p> </VelocityComponent> ) class VelocityApp extends Component { state = { letters: [],} onChange = (e) => { const letters = e.target.value.split(''); const arr = []; letters.forEach((l,i) => { arr.push(<VelocityLetter letter={l} />) }); this.setState({ letters: arr }); } render() { return ( <div className="App"> <div className="container"> <input onChange={this.onChange} style={styles.input} /> <div style={styles.letters}> { this.state.letters } </div> </div> </div> ); } } const styles = { input: { marginBottom: 20,padding: 8,height: 40,fontSize: 22,letters: { display: 'flex',height: 140,letter: { marginTop: 100,whiteSpace: 'pre',opacity: 0,} }
总结总的来说,基本的动画,我会选择
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |