Vue3+Vue Router使用<transition>过渡动画实现左右分栏后台布局

本文主要是介绍Vue3+Vue Router使用<transition>过渡动画实现左右分栏后台布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

摘要

利用Vue3及其配套的Vue Router实现后台管理系统中的页面过渡动画。文章首先简要介绍了Vue3的特性和Vue Router的基本用法,利用Vue3提供的组件以及Vue Router的路由钩子函数来实现页面过渡效果。

代码结构

components 里有4个组件,其中 Layout.vue 是左右分栏垂直布局组件,Apage.vue、Bpage.vue、Cpage.vue 分别是三个单独的页面,用于渲染在左右分栏布局的右侧,对应的是左侧导航的点击。

在这里插入图片描述

App.vue

<template><Layout />
</template><script>import './assets/main.css'; // 全局样式import Layout from './components/Layout.vue'; // 引入Layout组件export default {// 注册组件components: {Layout}};
</script>
Layout.vue

一般 Vue 路由设置如下所示:

<template><router-view />
</template>

在旧版本的 Vue 路由中,我们可以简单地用 <transition> 组件包装 <router-view>

但是,在较新版本的 Vue 路由中则必须用 v-slot 来解构 props 并将它们传递到我们的内部 slot 中。 这将包含一个动态组件,该组件被过渡组件包围。

<router-view v-slot="{ Component }"><transition><component :is="Component" /></transition>
</router-view>

完整代码:

<template><div class="container"><div class="left-pane"><!-- 左侧导航链接 --><div class="router-link"><router-link to="/" class="link">首页</router-link><router-link to="/Bpage" class="link">Bpage</router-link><router-link to="/Cpage" class="link">Cpage</router-link></div></div><div class="right-pane"><!-- 右侧内容 --><router-view v-slot="{ Component }"><transition name="fade" mode="out-in"><component :is="Component" /></transition></router-view></div></div>
</template><style>
.container {display: flex;flex-direction: row;height: 100vh; /* 100%视窗高度 */
}.left-pane {width: 250px;height: 100%; /* 100%父容器的高度 */box-sizing: border-box; /* 让边框不会撑大容器 */
}.right-pane {flex: 1; /* 平分父容器的宽度 */height: 100%; /* 100%父容器的高度 */box-sizing: border-box; /* 让边框不会撑大容器 */
}.left-pane {background-color: #eee; /* 左侧面板的背景色 */
}.router-link {width: 80%;margin: 22px auto 0;
}.link {width: 100;display: block;padding: 10px 0;text-align:center;text-decoration: none;color: #666;border-radius: 10px
}.right-pane {background-color: #ffffff; /* 右侧面板的背景色 */
}.fade-enter-active,
.fade-leave-active {transition: opacity 0.3s ease;
}.fade-enter-from,
.fade-leave-to {opacity: 0;
}.link.router-link-active {background-color: #ddd;color: #333;
}
</style>
Apage.vue
<template><div class="card"><h1>Index</h1></div>
</template><style scoped>
.card {width: 90%;height: 500px;background: #eee;margin: 20px auto;text-align: center;line-height: 500px;border-radius: 10px;
}
</style>
Bpage.vue
<template><div class="card"><h1>Bpage</h1></div>
</template><style scoped>
.card {width: 90%;height: 500px;background: #eee;margin: 20px auto;text-align: center;line-height: 500px;border-radius: 10px;
}
</style>
Cpage.vue
<template><div class="card"><h1>Cpage</h1></div>
</template><style scoped>
.card {width: 90%;height: 500px;background: #eee;margin: 20px auto;text-align: center;line-height: 500px;border-radius: 10px;
}
</style>
router/index.js
import { createRouter, createWebHashHistory  } from 'vue-router';const routes = [{path: '/',name: 'Apage',component: () => import('../components/Apage.vue'),},{path: '/Bpage',name: 'Bpage',component: () => import('../components/Bpage.vue'),},{path: '/Cpage',name: 'Cpage',component: () => import('../components/Cpage.vue'),},
];const router = createRouter({history: createWebHashHistory(),routes,linkActiveClass: 'router-link-active'
});export default router;

打包演示

https://demo.likeyunba.com/vue3-router-transition/

本文作者

TANKING

这篇关于Vue3+Vue Router使用<transition>过渡动画实现左右分栏后台布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/782711

相关文章

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M