React中路由切换动画

本文介绍了如何在React应用中使用`react-router-dom`实现路由切换时的动画效果,通过`CSSTransition`和`Animate`状态管理,确保在 '/' 和 '/wish' 之间的路径变化时,左右动画无缝过渡。
摘要由CSDN通过智能技术生成

react框架下App.js

import React from 'react'
import { Switch, Route, withRouter, Link, Redirect } from 'react-router-dom'
import './App.css';
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import Login from './components/Login'
import Wish from './components/Wish'
class App extends React.Component {
    state = {
        animate: "animateLeft"
    }
    componentWillReceiveProps(nextProps) {
        let to = nextProps.location.pathname
        let from = this.props.location.pathname
        console.log(to, from)
        if (to == "/" && from == "/wish" || to == "/wish" && from == "/") {
            // 加右动画
            this.state.animate = "animateRight"
        } else {
            // 加左动画
            this.state.animate = "animateLeft"
        }
    }
    shouldComponentUpdate(nextProps) {
        let to = nextProps.location.pathname
        let from = this.props.location.pathname
        if (to == from) {
            return false
        } else {
            return true
        }
    }
    render() {
        return (
            <div className="App">
                <TransitionGroup>
                    <CSSTransition timeout={2000} classNames={this.state.animate} unmountOnExit key={this.props.location.pathname}>
                        <Switch location={this.props.location}>
                            <Route exact path="/" component={Login} />
                            <Route path="/wish" component={Wish} />
                            <Redirect to="/"></Redirect>
                        </Switch>
                    </CSSTransition>
                </TransitionGroup>
            </div>
        );
    }
}


export default withRouter(App);

App.css

/* 入场动画过程 */
.animateLeft-enter{
  transform: translateX(100%) rotate(0) scale(0);
}
.animateLeft-enter-active{
  transition: all 2s;
  transform: translateX(0)  rotate(360deg) scale(1);
}
.animateLeft-enter-done{
  transform: translateX(0);
}
/* 出场动画过程 */
.animateLeft-exit{
  opacity: 1;
  /* transform: translateX(0px) */
}
.animateLeft-exit-active{
  opacity: 0;
  transition: 2s;
  /* transform: translateX(200px) */
}
.animateLeft-exit-done{
  opacity: 0;
  /* transform: translateX(200px) */
}


/* 入场动画过程 */
.animateRight-enter{
  transform: translateX(-100%) rotate(0) scale(0);
}
.animateRight-enter-active{
  transition: all 2s;
  transform: translateX(0)  rotate(360deg) scale(1);
}
.animateRight-enter-done{
  transform: translateX(0);
}
/* 出场动画过程 */
.animateRight-exit{
  opacity: 1;
  /* transform: translateX(0px) */
}
.animateRight-exit-active{
  opacity: 0;
  transition: 2s;
  /* transform: translateX(200px) */
}
.animateRight-exit-done{
  opacity: 0;
  /* transform: translateX(200px) */
}
React 实现手风琴切换动画可以使用 CSS 动画React 的状态管理来完成。以下是一个示例: 1. 创建一个手风琴组件 `Accordion`,并定义其状态 `activeIndex` 来记录当前展开的内容块的索引。 ```jsx import React, { useState } from 'react'; const Accordion = () => { const [activeIndex, setActiveIndex] = useState(null); const toggleAccordion = (index) => { setActiveIndex(activeIndex === index ? null : index); }; return ( <div className="accordion"> {data.map((item, index) => ( <div key={index} className={`accordion-item ${activeIndex === index ? 'active' : ''}`} onClick={() => toggleAccordion(index)} > <h3 className="accordion-title">{item.title}</h3> <div className="accordion-content">{item.content}</div> </div> ))} </div> ); }; export default Accordion; ``` 2. 使用 CSS 来定义动画效果。在这个示例,我们使用 CSS 的 `transition` 属性来实现展开和收起的过渡效果。 ```css .accordion-item { overflow: hidden; transition: height 0.3s; } .accordion-item.active { height: auto; } ``` 3. 在 `data` 数组存储每个内容块的标题和内容。 ```jsx const data = [ { title: '标题1', content: '内容1' }, { title: '标题2', content: '内容2' }, { title: '标题3', content: '内容3' }, ]; ``` 在上述代码,我们使用 `activeIndex` 来控制当前展开的内容块,点击标题时切换 `activeIndex` 的值,并根据 `activeIndex` 的值为对应的内容块添加或移除 `active` 类名。CSS 的过渡效果会在内容块的高度发生变化时自动触发。 你可以根据需要自定义样式和动画效果。希望这个示例对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北海屿鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值