当前位置:首页 > 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 动画定义:

.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 库

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

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 管理全局动画状态,实现跨组件协调的动画效果。

vue 实现跳转动画

// 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 类确保离开动画完整执行

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

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

h5实现网页跳转

h5实现网页跳转

H5实现网页跳转的方法 在HTML5中,可以通过多种方式实现网页跳转。以下是几种常见的方法: 使用<a>标签 通过超链接标签实现跳转是最传统的方式: <a href="http…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

vue实现跳转高亮

vue实现跳转高亮

Vue实现路由跳转高亮 在Vue项目中实现导航菜单跳转高亮效果,通常结合vue-router的active-class特性。以下是几种常见实现方式: 使用router-link的active-cla…

vue实现进入动画

vue实现进入动画

Vue 进入动画实现方法 使用 Vue 的过渡系统实现进入动画,主要通过 <transition> 或 <transition-group> 组件结合 CSS 或 JavaSc…

vue实现链接跳转

vue实现链接跳转

路由配置 在Vue项目中实现链接跳转通常使用Vue Router。需先在项目中安装并配置路由。通过vue-router库定义路由路径与组件的映射关系。 安装Vue Router: npm inst…