当前位置: 首页 > news >正文

Ruoyi-Vue拆解:优雅实现Vue页面过渡动画

🌟 前言

欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍

  • 🤖 洛可可白:个人主页

  • 🔥 个人专栏:✅前端技术 ✅后端技术

  • 🏠 个人博客:洛可可白博客

  • 🐱 代码获取:bestwishes0203

  • 📷 封面壁纸:洛可可白wallpaper

在这里插入图片描述

Vue页面过渡动画

  • Ruoyi-Vue拆解:优雅实现Vue页面过渡动画
    • Vue过渡系统基础
    • 完整实现代码
    • 关键实现解析
      • 1. 过渡组件配置
      • 2. CSS过渡动画
      • 3. 导航优化
    • 进阶技巧
      • 1. 动态过渡效果
      • 2. 集成第三方动画库
    • 性能优化建议

Ruoyi-Vue拆解:优雅实现Vue页面过渡动画

在现代Web应用中,流畅的页面过渡动画能显著提升用户体验。Vue.js提供了强大的过渡系统,让我们能够轻松实现各种页面切换效果。本文将通过一个完整的示例,带你了解如何实现类似Ruoyi-vue框架中的滑入滑出动画效果。

Vue过渡系统基础

Vue的<transition>组件是实现过渡效果的核心。当元素插入或移除时,Vue会自动应用CSS过渡类名:

  1. v-enter-from / v-leave-from: 进入/离开的起始状态
  2. v-enter-active / v-leave-active: 进入/离开的活动状态
  3. v-enter-to / v-leave-to: 进入/离开的结束状态

完整实现代码

在这里插入图片描述

<template>
  <div id="app">
    <div class="app-container">
      <nav class="sidebar">
        <div class="logo">Vue Transition</div>
        <ul class="nav-menu">
          <li 
            class="nav-item" 
            :class="{ active: $route.path === '/' }"
            @click="navigateTo('/')"
          >
            <span class="nav-icon">🏠</span>
            <span class="nav-text">首页</span>
          </li>
          <li 
            class="nav-item" 
            :class="{ active: $route.path === '/about' }"
            @click="navigateTo('/about')"
          >
            <span class="nav-icon">😊</span>
            <span class="nav-text">关于</span>
          </li>
        </ul>
      </nav>
      
      <main class="content-area">
        <transition name="slide-fade" mode="out-in">
          <router-view class="page-content" />
        </transition>
      </main>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    navigateTo(path) {
      if (this.$route.path !== path) {
        this.$router.push(path)
      }
    }
  }
}
</script>

<style>
/* 基础样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#app {
  height: 100vh;
  background: #f5f7fa;
}

.app-container {
  display: flex;
  height: 100%;
}

/* 侧边栏样式 */
.sidebar {
  width: 240px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px 0;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  padding: 0 20px 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.nav-menu {
  list-style: none;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.2);
  border-left: 3px solid white;
}

.nav-icon {
  margin-right: 10px;
  font-size: 1.1rem;
}

.nav-text {
  font-size: 0.95rem;
}

/* 内容区域样式 */
.content-area {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
}

.page-content {
  background: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  min-height: calc(100% - 60px);
}

/* 过渡动画效果 */
.slide-fade-enter-active {
  transition: all 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.slide-fade-enter-from {
  opacity: 0;
  transform: translateX(30px) scale(0.98);
}

.slide-fade-leave-to {
  opacity: 0;
  transform: translateX(-30px) scale(0.98);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    height: auto;
  }
  
  .content-area {
    padding: 20px;
  }
  
  .page-content {
    padding: 20px;
    min-height: auto;
  }
}
</style>

关键实现解析

1. 过渡组件配置

<transition name="slide-fade" mode="out-in">
  <router-view class="page-content" />
</transition>
  • name="slide-fade": 定义过渡类名前缀
  • mode="out-in": 确保当前元素先离开,新元素再进入

2. CSS过渡动画

.slide-fade-enter-active {
  transition: all 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

.slide-fade-enter-from {
  opacity: 0;
  transform: translateX(30px) scale(0.98);
}

.slide-fade-leave-to {
  opacity: 0;
  transform: translateX(-30px) scale(0.98);
}

这里使用了cubic-bezier曲线创建弹性效果,同时结合了平移和缩放动画,使过渡更加生动。

3. 导航优化

navigateTo(path) {
  if (this.$route.path !== path) {
    this.$router.push(path)
  }
}

添加了路由判断,避免重复导航触发不必要的动画。

进阶技巧

1. 动态过渡效果

可以根据路由深度应用不同的过渡方向:

watch: {
  $route(to, from) {
    const toDepth = to.path.split('/').length
    const fromDepth = from.path.split('/').length
    this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
  }
}

2. 集成第三方动画库

如使用animate.css

npm install animate.css

然后在组件中使用:

<transition
  enter-active-class="animate__animated animate__fadeInLeft"
  leave-active-class="animate__animated animate__fadeOutRight"
  mode="out-in"
>
  <router-view></router-view>
</transition>

性能优化建议

  1. 尽量使用CSS transforms和opacity属性,它们不会触发重排
  2. 避免在过渡中修改width/height等昂贵属性
  3. 合理设置过渡时长,通常在300-500ms之间
  4. 使用will-change属性提示浏览器优化

如果对你有帮助,点赞👍、收藏💖、关注🔔是我更新的动力!👋🌟🚀

http://www.dtcms.com/a/99013.html

相关文章:

  • 消息队列篇--通信协议篇--SSL/TLS协议
  • 【教学类-58-16】黑白三角拼图14——黑白三角图连接部分的白线(2*2宫格)
  • AI大模型底层技术——Multi-LoRA Combination Methods
  • 【免费】2007-2019年各省地方财政科学技术支出数据
  • leetcode 2360 图中最长的环 题解
  • 明天该穿哪件内衣出门?
  • 数据结构(并查集,图)
  • pip install cryptacular卡住,卡在downloading阶段
  • 嵌入式硬件篇---嘉立创PCB绘制
  • 【密码学】一文了解密码学的基本
  • 爱普生FC-135晶振5G手机的极端温度性能守护者
  • Ditto-Talkinghead:阿里巴巴数字人技术新突破 [特殊字符]️
  • Vue3组件响应式优化方法
  • Visual Studio 2022静态库与动态库创建及使用完全指南
  • Gerbv 与 Python 协同:实现 Gerber 文件智能分析与制造数据自动化
  • 知能行每日综测
  • vue.js前端条件渲染指令相关知识点
  • AI 时代,我们该如何写作?
  • MySQL———作业实验
  • Java进阶
  • 记录vite引入sass预编译报错error during build: [vite:css] [sass] Undefined variable.问题
  • MySQL的基础语法1(增删改查、DDL、DML、DQL和DCL)
  • HTML5 Web SQL 数据库学习笔记
  • 通信之光纤耦合器
  • cookie详解
  • comp2123 RangeFunc matrix
  • k8s网络策略
  • 从零开始搭建Anaconda环境
  • 网易邮箱DolphinScheduler迁移实战:从部署到优化,10倍效率提升的内部经验
  • plantsimulation编辑图标怎么把图标旋转90°