当前位置:首页 > React

react如何实现跳转过渡

2026-03-11 12:39:40React

实现页面跳转过渡的方法

在React中实现页面跳转过渡效果,通常需要结合路由库和动画库。以下是几种常见方法:

使用React Transition Group与React Router

安装必要依赖:

npm install react-transition-group react-router-dom

示例代码实现淡入淡出效果:

import { TransitionGroup, CSSTransition } from 'react-transition-group'
import { Switch, Route, useLocation } from 'react-router-dom'

function App() {
  const location = useLocation()

  return (
    <TransitionGroup>
      <CSSTransition
        key={location.key}
        timeout={300}
        classNames="fade"
      >
        <Switch location={location}>
          <Route path="/" exact component={Home} />
          <Route path="/about" component={About} />
        </Switch>
      </CSSTransition>
    </TransitionGroup>
  )
}

添加CSS过渡样式:

react如何实现跳转过渡

.fade-enter {
  opacity: 0;
}

.fade-enter-active {
  opacity: 1;
  transition: opacity 300ms;
}

.fade-exit {
  opacity: 1;
}

.fade-exit-active {
  opacity: 0;
  transition: opacity 300ms;
}

使用Framer Motion动画库

Framer Motion提供更强大的动画控制:

npm install framer-motion

示例实现滑动过渡:

import { AnimatePresence, motion } from 'framer-motion'
import { Switch, Route, useLocation } from 'react-router-dom'

function App() {
  const location = useLocation()

  return (
    <AnimatePresence exitBeforeEnter>
      <Switch location={location} key={location.pathname}>
        <Route path="/" exact>
          <motion.div
            initial={{ x: 300, opacity: 0 }}
            animate={{ x: 0, opacity: 1 }}
            exit={{ x: -300, opacity: 0 }}
            transition={{ duration: 0.5 }}
          >
            <Home />
          </motion.div>
        </Route>
      </Switch>
    </AnimatePresence>
  )
}

使用React Spring物理动画

React Spring基于物理属性实现更自然的过渡:

react如何实现跳转过渡

npm install react-spring

示例代码:

import { useTransition, animated } from 'react-spring'
import { Switch, Route, useLocation } from 'react-router-dom'

function App() {
  const location = useLocation()

  const transitions = useTransition(location, {
    from: { opacity: 0, transform: 'translate3d(100%,0,0)' },
    enter: { opacity: 1, transform: 'translate3d(0%,0,0)' },
    leave: { opacity: 0, transform: 'translate3d(-50%,0,0)' }
  })

  return transitions((props, item) => (
    <animated.div style={props}>
      <Switch location={item}>
        <Route path="/" exact component={Home} />
      </Switch>
    </animated.div>
  ))
}

自定义Hook实现页面过渡

创建自定义Hook管理过渡状态:

function usePageTransition() {
  const [isTransitioning, setIsTransitioning] = useState(false)

  const startTransition = useCallback(() => {
    setIsTransitioning(true)
    return new Promise(resolve => {
      setTimeout(() => {
        setIsTransitioning(false)
        resolve()
      }, 500)
    })
  }, [])

  return { isTransitioning, startTransition }
}

在组件中使用:

function Navigation() {
  const { startTransition } = usePageTransition()
  const history = useHistory()

  const navigate = async (path) => {
    await startTransition()
    history.push(path)
  }

  return (
    <button onClick={() => navigate('/about')}>
      Go to About
    </button>
  )
}

注意事项

  • 过渡时间应与实际路由切换时间匹配,避免动画未完成就显示新内容
  • 移动端需要考虑性能优化,复杂动画可能导致卡顿
  • 对于SEO敏感项目,确保过渡动画不会影响内容可抓取性
  • 测试不同路由切换场景,如前进/后退/直接访问等

以上方法可根据项目需求选择,简单过渡推荐React Transition Group,复杂动画推荐Framer Motion或React Spring。

分享给朋友:

相关文章

vue自动登录如何实现

vue自动登录如何实现

实现自动登录的基本思路 自动登录通常通过结合本地存储(如localStorage或cookie)和token验证机制实现。用户首次登录成功后,服务器返回的认证token会被保存在客户端,下次打开应用时…

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://example…

h5实现登录页面跳转页面跳转页面

h5实现登录页面跳转页面跳转页面

实现H5登录页面跳转 在H5中实现登录页面跳转可以通过多种方式完成,以下是几种常见方法: 使用window.location.href window.location.href = '目标页面UR…

h5实现页面跳转页面

h5实现页面跳转页面

H5 实现页面跳转的方法 在 H5(HTML5)中,实现页面跳转可以通过多种方式完成,以下是常见的几种方法: 使用 <a> 标签 通过 HTML 的 <a> 标签实现页面跳转…

vue如何实现单选

vue如何实现单选

使用原生 HTML 单选按钮 在 Vue 中可以直接使用 HTML 的原生单选按钮,通过 v-model 绑定数据。 <template> <div>…

如何实现翻页式h5

如何实现翻页式h5

翻页式H5的实现方法 翻页式H5通常指通过滑动或点击切换页面的交互形式,常用于营销活动、产品展示等场景。以下是几种常见的实现方式: 使用HTML5和CSS3实现基础翻页 通过CSS3的transfo…