Vue3实现面板分割

简介: Vue3实现面板分割

Vue3实现面板分割

下面是将你提供的 Vue 组件使用 SCSS,并以 Vue 3 的组合式 API 形式的面板分割代码。

1、建立组件相关的文件夹

2、将下面代码拷贝到index.vue中

<template>
  <div class="g-split" ref="gSplit">
    <!-- 水平方向 -->
    <div class="horizontal" v-if="showHorizontal">
      <div class="left-panel position" :style="horizontalLeftPanel">
        <slot name="left"></slot>
      </div>
      <div
        class="horizontal-trigger-panel position"
        :style="horizontaltriggerPanel"
        ref="horizontalTriggerPanel"
      >
        <slot name="trigger" v-if="$slots.trigger"></slot>
        <div class="trigger-content-default-wrap" v-else>
          <div class="trigger-content">
            <i class="trigger-bar" v-for="n in 7" :key="n"></i>
          </div>
        </div>
      </div>
      <div class="right-panel position" :style="horizontalRightPanel">
        <slot name="right"></slot>
      </div>
    </div>
    <!-- 垂直方向 -->
    <div class="vertical" v-if="showVertical">
      <div class="top-panel position" :style="verticalTopPanel">
        <slot name="top"></slot>
      </div>
      <div
        class="vertical-trigger-panel position"
        :style="verticaltriggerPanel"
        ref="verticalTriggerPanel"
      >
        <slot name="trigger" v-if="$slots.trigger"></slot>
        <div class="trigger-content-default-wrap" v-else>
          <div class="trigger-content">
            <i class="trigger-bar" v-for="n in 7" :key="n"></i>
          </div>
        </div>
      </div>
      <div class="bottom-panel position" :style="verticalBottomPanel">
        <slot name="bottom"></slot>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue';
// 注意:页面偏移量单位统一使用百分比计算
const props = defineProps({
  value: {
    type: [String, Number],
    default: 0.5,
  },
  mode: {
    type: String,
    validator(value) {
      return ["horizontal", "vertical"].includes(value);
    },
    default: "horizontal",
  },
  min: {
    type: [String, Number],
    default: 0.1,
  },
  max: {
    type: [String, Number],
    default: 0.9,
  },
});
import { defineEmits } from 'vue';
const emit = defineEmits(['onMoveStart', 'onMoving', 'onMoveEnd']);
const gSplit = ref(null);
const horizontalTriggerPanel = ref(null);
const verticalTriggerPanel = ref(null);
const left = ref(props.value);
const top = ref(props.value);
const gSplitWidth = ref(0);
const gSplitHeight = ref(0);
const horizontalTriggerPanelWidth = ref(0);
const verticalTriggerPanelHeight = ref(0);
const showHorizontal = computed(() => props.mode === "horizontal");
const showVertical = computed(() => props.mode === "vertical");
const horizontalLeftPanel = computed(() => ({
  left: 0,
  right: (1 - left.value) * 100 + "%",
}));
const horizontalRightPanel = computed(() => ({
  left: (left.value + horizontalTriggerPanelWidth.value / gSplitWidth.value) * 100 + "%",
}));
const horizontaltriggerPanel = computed(() => ({
  left: left.value * 100 + "%",
}));
const verticalTopPanel = computed(() => ({
  top: 0,
  bottom: (1 - top.value) * 100 + "%",
}));
const verticalBottomPanel = computed(() => ({
  top: (top.value + verticalTriggerPanelHeight.value / gSplitHeight.value) * 100 + "%",
}));
const verticaltriggerPanel = computed(() => ({
  top: top.value * 100 + "%",
}));
const initDom = () => {
  gSplitWidth.value = gSplit.value.clientWidth;
  gSplitHeight.value = gSplit.value.clientHeight;
  if (props.mode === "horizontal") {
    horizontalTriggerPanelWidth.value = horizontalTriggerPanel.value.clientWidth;
  } else {
    verticalTriggerPanelHeight.value = verticalTriggerPanel.value.clientHeight;
  }
};
const bindEvent = () => {
  if (props.mode === "horizontal") {
    bindHorizontalTriggerPanelEvent();
  } else {
    bindVerticalTriggerPanelEvent();
  }
};
const preventSelectedOnMouseMove = (e) => e.preventDefault();
const bindHorizontalTriggerPanelEvent = () => {
  resolveMouseFn("horizontal", horizontalTriggerPanel.value);
};
const bindVerticalTriggerPanelEvent = () => {
  resolveMouseFn("vertical", verticalTriggerPanel.value);
};
const resolveMouseFn = (type, element) => {
  const mousedown = (e) => {
    document.addEventListener("selectstart", preventSelectedOnMouseMove);
    emit("onMoveStart", e);
    const pos = type === "horizontal" ? "left" : "top";
    const distance = type === "horizontal" ? e.clientX - element.offsetLeft : e.clientY - element.offsetTop;
    const mousemove = (e) => {
      emit("onMoving", e);
      const gSplitSize = type === "horizontal" ? gSplitWidth.value : gSplitHeight.value;
      const newPos = (type === "horizontal" ? e.clientX - distance : e.clientY - distance) / gSplitSize;
      if (newPos < props.min) newPos = props.min;
      if (newPos > 1 - props.min) newPos = 1 - props.min;
      if (pos === "left") left.value = newPos;
      else top.value = newPos;
    };
    const mouseup = () => {
      emit("onMoveEnd", e);
      document.removeEventListener("mousemove", mousemove);
      document.removeEventListener("mouseup", mouseup);
      document.removeEventListener("selectstart", preventSelectedOnMouseMove);
    };
    document.addEventListener("mousemove", mousemove);
    document.addEventListener("mouseup", mouseup);
  };
  element.addEventListener("mousedown", mousedown);
};
onMounted(() => {
  bindEvent();
  initDom();
});
</script>
<style lang="scss">
.g-split {
  height: 100%;
  overflow: hidden;
  .position {
    position: absolute;
  }
  .horizontal {
    position: relative;
    height: 100%;
    .left-panel, .right-panel {
      height: 100%;
    }
    .horizontal-trigger-panel {
      cursor: col-resize;
      height: 100%;
      .trigger-content-default-wrap {
        background-color: #f8f8f9;
        height: 100%;
        position: relative;
        width: 7px;
        .trigger-content {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          .trigger-bar {
            width: 7px;
            height: 1px;
            display: block;
            background: rgba(23, 35, 61, 0.25);
            margin-top: 3px;
          }
        }
      }
    }
  }
  .vertical {
    position: relative;
    height: 100%;
    .top-panel, .bottom-panel {
      width: 100%;
    }
    .vertical-trigger-panel {
      width: 100%;
      .trigger-content-default-wrap {
        width: 100%;
        position: relative;
        height: 7px;
        cursor: row-resize;
        background-color: #f8f8f9;
        .trigger-content {
          position: absolute;
          left: 50%;
          top: 0;
          transform: translateX(-50%);
          height: 100%;
          .trigger-bar {
            width: 1px;
            height: 100%;
            display: inline-block;
            background: rgba(23, 35, 61, 0.25);
            margin-left: 3px;
            vertical-align: top;
          }
        }
      }
    }
  }
}
</style>

使用

注册组件

在你的 Vue 项目中,你需要在你的组件或应用程序中注册 GSplitPanel 组件。你可以通过局部注册或全局注册来实现。

局部注册

在你的父组件中导入并注册 GSplitPanel 组件:

<template>
  <div>
    <GSplitPanel mode="horizontal" :value="0.5">
      <template v-slot:left>
        <div>左侧面板内容</div>
      </template>
      <template v-slot:right>
        <div>右侧面板内容</div>
      </template>
    </GSplitPanel>
  </div>
</template>
<script setup>
import GSplitPanel from '@/components/Split/index.vue';
</script>
全局注册

如果你希望在整个项目中使用 GSplitPanel 组件,可以在你的主入口文件(如 main.js 或 main.ts)中全局注册:

import { createApp } from 'vue';
import App from './App.vue';
import GSplitPanel from '@/components/Split/index.vue';
const app = createApp(App);
app.component('GSplitPanel', GSplitPanel);
app.mount('#app');

使用组件

在你的项目中,你现在可以使用 标签来使用这个组件。你可以通过 mode 属性来控制是水平分割还是垂直分割,并通过插槽来填充面板的内容。

vue
<GSplitPanel mode="horizontal" :value="0.5">
  <template v-slot:left>
    <div>左侧面板内容</div>
  </template>
  <template v-slot:right>
    <div>右侧面板内容</div>
  </template>
</GSplitPanel>
相关文章
|
5月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
814 139
|
10月前
|
缓存 JavaScript PHP
斩获开发者口碑!SnowAdmin:基于 Vue3 的高颜值后台管理系统,3 步极速上手!
SnowAdmin 是一款基于 Vue3/TypeScript/Arco Design 的开源后台管理框架,以“清新优雅、开箱即用”为核心设计理念。提供角色权限精细化管理、多主题与暗黑模式切换、动态路由与页面缓存等功能,支持代码规范自动化校验及丰富组件库。通过模块化设计与前沿技术栈(Vite5/Pinia),显著提升开发效率,适合团队协作与长期维护。项目地址:[GitHub](https://github.com/WANG-Fan0912/SnowAdmin)。
1219 5
|
5月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
434 1
|
6月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
710 12
|
5月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
497 0
|
7月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
731 1
|
7月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
413 0
|
8月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
200 0
|
10月前
|
JavaScript API 容器
Vue 3 中的 nextTick 使用详解与实战案例
Vue 3 中的 nextTick 使用详解与实战案例 在 Vue 3 的日常开发中,我们经常需要在数据变化后等待 DOM 更新完成再执行某些操作。此时,nextTick 就成了一个不可或缺的工具。本文将介绍 nextTick 的基本用法,并通过三个实战案例,展示它在表单验证、弹窗动画、自动聚焦等场景中的实际应用。
981 17
|
10月前
|
JavaScript 前端开发 API
Vue 2 与 Vue 3 的区别:深度对比与迁移指南
Vue.js 是一个用于构建用户界面的渐进式 JavaScript 框架,在过去的几年里,Vue 2 一直是前端开发中的重要工具。而 Vue 3 作为其升级版本,带来了许多显著的改进和新特性。在本文中,我们将深入比较 Vue 2 和 Vue 3 的主要区别,帮助开发者更好地理解这两个版本之间的变化,并提供迁移建议。 1. Vue 3 的新特性概述 Vue 3 引入了许多新特性,使得开发体验更加流畅、灵活。以下是 Vue 3 的一些关键改进: 1.1 Composition API Composition API 是 Vue 3 的核心新特性之一。它改变了 Vue 组件的代码结构,使得逻辑组
2188 0