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

news/2024/6/18 21:39:55 标签: vue.js, 前端, javascript

摘要

利用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


http://www.niftyadmin.cn/n/5420728.html

相关文章

【原创教程】S7-1200配方程序编写方法

1 绪论 1.1 本文的目的 在生产中我们的一台设备往往需要 对应很多种不同工艺或不同尺寸的设备,这就要求我们设备的参数需要经常变化。我们将每一种产品对应的参数保存起来,下一次再生产同种产品时可以迅速一键调用,而不是一个一个的去设置,这种功能就叫做配方(Recipe)。…

3.10 log | 647. 回文子串

647. 回文子串&#xff0c;516.最长回文子序列 class Solution { public:int countSubstrings(string s) {vector<vector<bool>> dp(s.size(),vector<bool>(s.size(),false));int result0;for(int is.size()-1;i>0;i--){for(int ji;j<s.size();j){if(…

POS 之 奖励机制

为什么需要有奖惩机制 如果没有奖励&#xff0c;就不会有节点参与POS&#xff0c;运营节点有成本&#xff0c;而奖励正是让运营者获利的方式 如果没有惩罚&#xff0c;网络上会充斥着很多无效节点&#xff0c;会扰乱甚至破坏网络 所有奖励和惩罚在每个 Epoch 实施一次 奖励 什…

鸿蒙OS应用开发之显示图片组件11

前面学习了像素降级处理的方法,这样方便一个图片可以显示在不同大小屏幕的技术,同样不会失真。现在来学习另外一个重要的技术,就是图片处理。图片处理是一个很范的名词,一般来说图片处理都会采用预处理的方法,比如在电脑上采用图形处理软件进行处理,然后再使用到手机的软…

深入理解React中的useReducer:管理复杂状态逻辑的利器

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

Spring MVC配置MyBatis vs. Spring Boot配置MyBatis

在Java Web开发中&#xff0c;MyBatis是一个常用的持久层框架&#xff0c;用于简化数据库访问操作。在Spring框架中&#xff0c;我们可以通过Spring MVC和Spring Boot两种方式来集成MyBatis&#xff0c;本文将比较这两种方式的优缺点&#xff0c;并展示它们的具体代码实现。 S…

HTML静态网页成品作业(HTML+CSS)——家乡漳州介绍设计制作(1个页面)

&#x1f389;不定期分享源码&#xff0c;关注不丢失哦 文章目录 一、作品介绍二、作品演示三、代码目录四、网站代码HTML部分代码 五、源码获取 一、作品介绍 &#x1f3f7;️本套采用HTMLCSS&#xff0c;未使用Javacsript代码&#xff0c;共有1个页面。 二、作品演示 三、代…

AI辅助研发

随着人工智能技术的持续发展与突破&#xff0c;2024年AI辅助研发正成为科技界和工业界瞩目的焦点。从医药研发到汽车设计&#xff0c;从软件开发到材料科学&#xff0c;AI正逐渐渗透到研发的各个环节&#xff0c;变革着传统的研发模式。在这一背景下&#xff0c;AI辅助研发不仅…