vue3+ts 拖拽容器边缘,改变容器宽度和高度

例如:我们的代码编辑器

终端与代码区,可以纵向拖拽,改变两个容器高度

目录与代码区可以横向拖拽,改变两个容器宽度

本文使用vue3+ts+tailwindcss,把横向纵向整合在一起写了,也可以分开使用
utils目录下新建一个drag.ts文件

import { Ref } from 'vue'

interface ResizeOptions {
  rightRef?: Ref<HTMLElement | null>
  bottomRef?: Ref<HTMLElement | null>
}

const useResize = ({ rightRef, bottomRef }: ResizeOptions) => {
  let startX = 0
  let startWidth = 0
  let isHorizonResizing = false
  let startY = 0
  let startHeight = 0
  let isVerticalResizing = false

  const startHorizonResize = (event: MouseEvent) => {
    if (rightRef && rightRef.value) {
      startX = event.clientX
      startWidth = rightRef.value.offsetWidth
      isHorizonResizing = true

      window.addEventListener('mousemove', doHorizonResize)
      window.addEventListener('mouseup', stopHorizonResize)
    }
  }

  const doHorizonResize = (event: MouseEvent) => {
    if (isHorizonResizing && rightRef && rightRef.value) {
      const deltaX = event.clientX - startX
      rightRef.value.style.width = startWidth - deltaX + 'px'
    }
  }

  const stopHorizonResize = () => {
    isHorizonResizing = false
    window.removeEventListener('mousemove', doHorizonResize)
    window.removeEventListener('mouseup', stopHorizonResize)
  }

  const startVerticalResize = (event: MouseEvent) => {
    if (bottomRef && bottomRef.value) {
      startY = event.clientY
      startHeight = bottomRef.value.offsetHeight
      isVerticalResizing = true
      window.addEventListener('mousemove', doVerticalResize)
      window.addEventListener('mouseup', stopVerticalResize)
    }
  }

  const doVerticalResize = (event: MouseEvent) => {
    if (isVerticalResizing && bottomRef && bottomRef.value) {
      const deltaY = event.clientY - startY
      bottomRef.value.style.height = startHeight - deltaY + 'px'
    }
  }

  const stopVerticalResize = () => {
    isVerticalResizing = false
    window.removeEventListener('mousemove', doVerticalResize)
    window.removeEventListener('mouseup', stopVerticalResize)
  }

  return {
    startHorizonResize,
    startVerticalResize,
  }
}

export default useResize

使用

<template>
  <div class="h-full w-full flex">
    <div class="flex-1 h-full min-w-[100px] w-[300px] bg-[#c1ddfb]"></div>
    <div class="cursor-col-resize h-full w-[2px] bg-[#000]" @mousedown.stop.prevent="startHorizonResize"></div>
    <div class="h-full w-[calc(100%-300px)] min-w-[500px]" ref="refRight">
      <div class="w-full h-full flex flex-col">
        <div class="flex-1 bg-[#ebbbdd] h-[400px] min-h-[100px] w-full"></div>
        <div class="cursor-row-resize h-[2px] bg-[#333] w-full" @mousedown.stop.prevent="startVerticalResize"></div>
        <div class="bg-[#c0eaab] h-[calc(100%-400px)] min-h-[100px] w-full" ref="refBottom"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import useResize from '@/utils/drag'

const refRight = ref<HTMLElement | null>(null)
const refBottom = ref<HTMLElement | null>(null)

const { startHorizonResize, startVerticalResize } = useResize({ rightRef: refRight, bottomRef: refBottom })
</script>

<style scoped></style>

相关推荐

  1. el-table vxe-table 表格宽度

    2024-06-07 20:00:04       32 阅读
  2. Vue——vue3库Sortablejs

    2024-06-07 20:00:04       58 阅读
  3. vue3组件vuedraggable

    2024-06-07 20:00:04       67 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 20:00:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 20:00:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 20:00:04       87 阅读
  4. Python语言-面向对象

    2024-06-07 20:00:04       96 阅读

热门阅读

  1. reshape用法 python:深入探索多维数组的重塑技巧

    2024-06-07 20:00:04       22 阅读
  2. 一篇高效处理数据可视化Python库,看这篇就够了

    2024-06-07 20:00:04       34 阅读
  3. gpt4free软件的 g4f gui 网页速度非常慢的问题解决

    2024-06-07 20:00:04       26 阅读
  4. 深度解析 VPN 工作原理:保护隐私的关键

    2024-06-07 20:00:04       25 阅读
  5. Podman:Linux下的无守护进程容器引擎

    2024-06-07 20:00:04       30 阅读
  6. NLP基础——语言模型(动手学深度学习)

    2024-06-07 20:00:04       25 阅读
  7. 【怀旧版】win10中从零开始创建vue2+ElementUI项目

    2024-06-07 20:00:04       30 阅读
  8. 【实用技巧】Unity的Transform组件实用技巧

    2024-06-07 20:00:04       25 阅读
  9. 每日一题:聊聊 Redis 过期键的删除策略

    2024-06-07 20:00:04       29 阅读
  10. 函数或变量 ‘tfrstft‘ 无法识别

    2024-06-07 20:00:04       29 阅读
  11. 新能源汽车企业的图纸防泄密解决方案

    2024-06-07 20:00:04       23 阅读
  12. 使用React Hooks有什么优势

    2024-06-07 20:00:04       25 阅读
  13. 笔记93:关于 C++ 中的 Eigen 库

    2024-06-07 20:00:04       31 阅读