当前位置:首页 > VUE

vue 实现跳转动画

2026-02-22 17:01:20VUE

Vue 实现跳转动画的方法

使用 Vue Router 的过渡效果

Vue Router 内置支持过渡效果,可以通过 <transition> 组件结合 CSS 实现页面跳转动画。在路由视图外层包裹 <transition> 并定义动画样式。

<transition name="fade" mode="out-in">
  <router-view></router-view>
</transition>

CSS 动画定义:

vue 实现跳转动画

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

动态路由过渡

根据路由变化动态设置过渡效果,通过监听路由变化修改过渡名称。

<transition :name="transitionName">
  <router-view></router-view>
</transition>
data() {
  return {
    transitionName: 'slide-left'
  }
},
watch: {
  '$route'(to, from) {
    const toDepth = to.meta.depth
    const fromDepth = from.meta.depth
    this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
  }
}

使用 Animate.css 库

集成第三方动画库快速实现丰富效果,安装后直接应用预设动画类。

vue 实现跳转动画

npm install animate.css
<transition
  enter-active-class="animate__animated animate__fadeIn"
  leave-active-class="animate__animated animate__fadeOut"
>
  <router-view></router-view>
</transition>

组件内守卫控制动画

利用路由守卫在跳转前后执行动画逻辑,适用于需要精细控制的场景。

beforeRouteEnter(to, from, next) {
  // 进入前触发动画
  document.querySelector('.page-container').classList.add('fade-out')
  setTimeout(() => next(), 500)
},
beforeRouteLeave(to, from, next) {
  // 离开前触发动画
  this.$el.classList.add('fade-out')
  setTimeout(next, 500)
}

状态管理配合动画

通过 Vuex 管理全局动画状态,实现跨组件协调的动画效果。

// store.js
state: {
  isLoading: false
},
mutations: {
  setLoading(state, payload) {
    state.isLoading = payload
  }
}
// 路由跳转时
this.$store.commit('setLoading', true)
setTimeout(() => {
  this.$router.push('/target')
  this.$store.commit('setLoading', false)
}, 1000)

注意事项

  • 过渡模式 mode="out-in" 可防止相邻路由组件同时执行动画
  • 移动端注意硬件加速优化,使用 transform 替代 top/left 属性
  • 复杂动画考虑使用 GSAP 等专业动画库
  • 始终定义 leave-active 类确保离开动画完整执行

标签: 跳转动画
分享给朋友:

相关文章

利用css3制作动画

利用css3制作动画

CSS3 动画基础 CSS3 通过 @keyframes 规则和 animation 属性实现动画效果。@keyframes 定义动画的关键帧,animation 控制动画的播放方式(如持续时间、延迟…

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进…

js实现页面跳转

js实现页面跳转

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

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

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

实现登录页面跳转的方法 在H5中实现登录页面跳转可以通过多种方式完成,以下是一些常见的方法: 使用JavaScript的window.location方法 通过JavaScript的window.l…

jquery动画

jquery动画

jQuery动画基础 jQuery提供了多种动画方法,用于创建平滑的过渡效果。常用的动画方法包括animate()、fadeIn()、fadeOut()、slideUp()和slideDown()。…

vue实现点击跳转

vue实现点击跳转

路由跳转(Vue Router) 在Vue项目中通过vue-router实现页面跳转是最常见的方式。确保已安装并配置路由: // 路由配置示例(router/index.js) import {…