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>
相关推荐
临枫3881 分钟前
网页图像优化:现代格式与响应式技巧
前端
咪库咪库咪12 分钟前
构建交互网站
前端
周星星日记13 分钟前
10.vue3中组件实现原理(上)
前端·vue.js·面试
小华同学ai14 分钟前
6.4K star!轻松搞定专业领域大模型推理,这个知识增强框架绝了!
前端·github
萧门竹巷17 分钟前
里面有猫!Contenteditable 实现简单的富文本编辑器!
javascript
专业抄代码选手17 分钟前
【VUE】在vue中,Watcher与Dep的关系
前端·面试
Lazy_zheng21 分钟前
从 DOM 监听到 Canvas 绘制:一套完整的水印实现方案
前端·javascript·面试
尘寰ya23 分钟前
前端面试-微前端
前端·面试·职场和发展
蘑菇头爱平底锅24 分钟前
数字孪生-DTS-孪创城市-前端实现动态地铁分布线路图
前端·javascript·数据可视化
风中飘爻25 分钟前
JavaScript:表单及正则表达式验证
开发语言·javascript·ecmascript