react-transition-group

**

1、 CSSTransition实现单一组件动画

**
CSSTransition单独使用时,有两个比较重要的属性。in和classNames。
classNames属性: CSSTransition子组件动态类选择器名前缀。
in属性

  • 当in为true时,CSSTransition的子组件会先添加 c l a s s N a m e s − e n t e r 类 , 下 一 个 t i c k 会 添 加 {classNames}-enter类,下一个tick会添加 classNamesentertick{classNames}-enter-active类。
  • 当in为false时,CSSTransition的子组件会先添加 c l a s s N a m e s − e x i t 类 , 下 一 个 t i c k 会 添 加 {classNames}-exit类,下一个tick会添加 classNamesexittick{classNames}-exit-active类。

小DEMO

export default class DemoApp2 extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            number: 0
        };
        this.handleSwitch = this.handleSwitch.bind(this);
    }
    handleSwitch(event) {
        this.setState({
            number: this.state.number === 0?1:0
        });
    }
    render() {
        return (
            <div className='app2-container'>
                <TransitionGroup>
                    <CSSTransition
                        key={this.state.number}
                        timeout={500}
                        classNames='app2'
                        unmountOnExit={true}
                    >
                        <div className='app2-square'>{this.state.number}</div>
                    </CSSTransition>
                </TransitionGroup>
                <button className='app2-btn' onClick={this.handleSwitch}>切换</button>
            </div>
        );
    }
}
.app1-enter {
    /*初始透明度为0*/
    opacity: 0;
    /*初始偏移量为100%*/
    transform: translateX(100%);
}

.app1-enter-active {
    /*随后透明度为1*/
    opacity: 1;
    /*随后偏移量回归原位*/
    transform: translateX(0);
    /*这个移动过程我们做个动画,动画持续时长500毫秒*/
    transition: all 500ms ease;
}

.app1-exit {
    opacity: 1;
    transform: translateX(0);
}

.app1-exit-active {
    opacity: 0;
    transform: translateX(-100%);
    transition: all 500ms ease;
}

效果显示
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值