vue3后端管理项目,左侧菜单可以拖拽调整宽度

javascript 复制代码
<!-- 纵向布局 -->
<template>
  <el-container class="layout m-layout">
    <el-aside>
      <div class="aside-box" :style="{ width: isCollapse ? '65px' : asideWidth + 'px' }">
        <div class="logo flx-center">
          <img class="logo-img" src="@/assets/images/logo.png" alt="logo" />
          <span v-show="!isCollapse" class="logo-text">{{ title }}</span>
        </div>
        <el-scrollbar>
          <el-menu
            :router="false"
            :default-active="activeMenu"
            :collapse="isCollapse"
            :unique-opened="accordion"
            :collapse-transition="false"
          >
            <SubMenu :menu-list="menuList" />
          </el-menu>
        </el-scrollbar>
      </div>
      <div class="m-drag" @mousedown="startResizing"></div>
    </el-aside>
    <el-container>
      <el-header>
        <ToolBarLeft />
        <ToolBarRight />
      </el-header>
      <Main />
    </el-container>
  </el-container>
</template>

<script setup lang="ts" name="layoutVertical">
import { computed, ref, onMounted, onUnmounted } from "vue";
import { useRoute } from "vue-router";
import { useAuthStore } from "@/stores/modules/auth";
import { useGlobalStore } from "@/stores/modules/global";
import Main from "@/layouts/components/Main/index.vue";
import ToolBarLeft from "@/layouts/components/Header/ToolBarLeft.vue";
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";

const title = import.meta.env.VITE_GLOB_APP_TITLE;

const route = useRoute();
const authStore = useAuthStore();
const globalStore = useGlobalStore();
const accordion = computed(() => globalStore.accordion);
const isCollapse = computed(() => globalStore.isCollapse);
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path) as string);

const asideWidth = ref<number>(210);
let isResizing = false;
let startX = 0;
let startWidth = 0;

const startResizing = (event: MouseEvent) => {
  isResizing = true;
  startX = event.clientX;
  startWidth = asideWidth.value;

  document.addEventListener("mousemove", resizeAside);
  document.addEventListener("mouseup", stopResizing);
};

const resizeAside = (event: MouseEvent) => {
  if (isResizing) {
    const newWidth = startWidth + (event.clientX - startX);
    asideWidth.value = newWidth > 50 ? newWidth : 50; // 设置最小宽度为 50px
  }
};

const stopResizing = () => {
  isResizing = false;
  document.removeEventListener("mousemove", resizeAside);
  document.removeEventListener("mouseup", stopResizing);
};

onMounted(() => {
  document.addEventListener("mouseup", stopResizing);
});

onUnmounted(() => {
  document.removeEventListener("mouseup", stopResizing);
});
</script>

<style scoped lang="scss">
@import "./index.scss";
</style>
相关推荐
mCell1 天前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip1 天前
Node.js 子进程:child_process
前端·javascript
excel1 天前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel1 天前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼1 天前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping1 天前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙1 天前
[译] Composition in CSS
前端·css
白水清风1 天前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix1 天前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278001 天前
new、原型和原型链浅析
前端·javascript