当前位置: 首页 > news >正文

Vue3实现拖拽改变元素大小

代码实现

整体页面结构通过一个 dragResize-wrapper 包含左右两个区域,左侧区域包含一个可拖拽的边界。以下是关键代码

HTML 部分
<template><div class="dragResize-wrapper"><div class="dragResize-left"><div class="resize" @mousedown="resize"></div></div><div class="dragResize-right"></div></div>
</template>
CSS 部分
<style lang="less" scoped>
.dragResize-wrapper {display: flex;width: 100vw;height: 100vh;.dragResize-left {position: relative;width: 30%;height: 100%;background-color: #1a2029;border-right: 1px solid #ccc;.resize {position: absolute;right: 0;width: 10px;height: 100%;background-color: #1a2029;}.resize:hover {cursor: e-resize;}}.dragResize-right {width: 70%;height: 100%;background-color: #472020;}
}
</style>
JavaScript 部分
<script setup>
import { ref, reactive, onMounted } from "vue";
const dragState = reactive({isDragging: false,startX: 0,leftInitialWidth: 0,minWidth: 100, // 左侧区域最小宽度限制maxWidth: 800, // 左侧区域最大宽度限制
});const resize = (e) => {dragState.isDragging = true;dragState.startX = e.clientX; // 获取鼠标按下时的 X 坐标dragState.leftInitialWidth = e.currentTarget.parentElement.offsetWidth; // 获取左侧区域初始宽度document.body.style.cursor = "e-resize"; // 设置鼠标样式为拖拽样式document.body.style.userSelect = "none"; // 禁止选中文本
};// 鼠标移动事件,执行拖拽调整宽度
const handleMouseMove = (e) => {if (!dragState.isDragging) return; // 如果没有拖拽,直接返回const deltax = e.clientX - dragState.startX; // 计算鼠标移动的距离let newWidth = dragState.leftInitialWidth + deltax; // 计算新的宽度newWidth = Math.max(dragState.minWidth,Math.min(newWidth, dragState.maxWidth)); // 限制宽度在最小和最大值之间document.querySelector(".dragResize-left").style.width = `${newWidth}px`; // 设置左侧区域的新宽度document.querySelector(".dragResize-right").style.width = `calc(100% - ${newWidth}px)`; // 设置右侧区域宽度为剩余空间
};// 鼠标松开事件,结束拖拽
const handleMouseUp = () => {if (dragState.isDragging) {dragState.isDragging = false; // 重置拖拽状态document.body.style.cursor = "default"; // 恢复鼠标样式document.body.style.userSelect = "auto"; // 恢复文本选择}
};onMounted(() => {document.addEventListener("mousemove", handleMouseMove); // 监听鼠标移动事件document.addEventListener("mouseup", handleMouseUp); // 监听鼠标松开事件
});
</script>

定义一个 dragState 对象来跟踪拖拽状态,包括是否正在拖拽、鼠标起始坐标、左侧区域初始宽度以及左右宽度限制。在 resize 函数中,设置拖拽开始时的相关状态和样式。handleMouseMove 函数根据鼠标移动距离计算新的宽度,并在一定范围内调整左右区域的宽度。handleMouseUp 函数用于结束拖拽并恢复样式。在组件挂载后添加鼠标移动和松开的事件监听。

完整实例代码

<template><div class="dragResize-wrapper"><div class="dragResize-left"><div class="resize" @mousedown="resize"></div></div><div class="dragResize-right"></div></div>
</template><script setup>
import { ref, reactive, onMounted } from "vue";
const dragState = reactive({isDragging: false,startX: 0,leftInitialWidth: 0,minWidth: 100, // 左侧区域最小宽度限制maxWidth: 800, // 左侧区域最大宽度限制
});const resize = (e) => {dragState.isDragging = true;dragState.startX = e.clientX; // 获取鼠标按下时的X坐标dragState.leftInitialWidth = e.currentTarget.parentElement.offsetWidth; // 获取左侧区域初始宽度document.body.style.cursor = "e-resize"; // 设置鼠标样式为拖拽样式document.body.style.userSelect = "none"; // 禁止选中文本
};// 鼠标移动事件,执行拖拽调整宽度
const handleMouseMove = (e) => {if (!dragState.isDragging) return; // 如果没有拖拽,直接返回const deltax = e.clientX - dragState.startX; // 计算鼠标移动的距离let newWidth = dragState.leftInitialWidth + deltax; // 计算新的宽度newWidth = Math.max(dragState.minWidth,Math.min(newWidth, dragState.maxWidth)); // 限制宽度在最小和最大值之间document.querySelector(".dragResize-left").style.width = `${newWidth}px`; // 设置左侧区域的新宽度document.querySelector(".dragResize-right").style.width = `calc(100% - ${newWidth}px)`; // 设置右侧区域宽度为剩余空间
};// 鼠标松开事件,结束拖拽
const handleMouseUp = () => {if (dragState.isDragging) {dragState.isDragging = false; //重置拖拽状态document.body.style.cursor = "default"; // 恢复鼠标样式document.body.style.userSelect = "auto"; // 恢复文本选择}
};onMounted(() => {document.addEventListener("mousemove", handleMouseMove); // 监听鼠标移动事件document.addEventListener("mouseup", handleMouseUp); // 监听鼠标松开事件
});
</script><style lang="less" scoped>
.dragResize-wrapper {display: flex;width: 100vw;height: 100vh;.dragResize-left {position: relative;width: 30%;height: 100%;background-color: #1a2029;border-right: 1px solid #ccc;.resize {position: absolute;right: 0;width: 10px;height: 100%;background-color: #1a2029;}.resize:hover {cursor: e-resize;}}.dragResize-right {width: 70%;height: 100%;background-color: #472020;}
}
</style>

http://www.lqws.cn/news/164395.html

相关文章:

  • 1.2 fetch详解
  • React Hooks 指南:何时使用 useEffect ?
  • Grafana-ECharts应用讲解(玫瑰图示例)
  • Vue2数组数字字段求和技巧 数字求和方法
  • Compose Multiplatform 实现自定义的系统托盘,解决托盘乱码问题
  • Postman接口测试之postman设置接口关联,实现参数化
  • MATLAB仿真:偏振光在光纤通信中的应用研究_可复现,有问题请联系博主
  • 从内核到应用层:Linux缓冲机制与语言缓冲区的协同解析
  • Spring AI学习一
  • StoreView SQL,让数据分析不受地域限制
  • mysql复合查询mysql子查询
  • 如何通过外网访问内网?哪个方案比较好用?跨网远程连接网络知识早知道
  • Struts2漏洞由浅入深
  • Python语法进阶篇 --- 封装、继承、多态
  • React从基础入门到高级实战:React 实战项目 - 项目二:电商平台前端
  • 大中型水闸安全监测管理系统建设方案
  • 工厂模式 + 模板方法模式 + 策略模式的适用场景
  • 在 Spring Boot 中使用 JSP
  • 如何做好一份技术文档?(下篇)
  • Ansys Maxwell:线圈和磁体的静磁 3D 分析
  • 每日算法 -【Swift 算法】三数之和最接近目标值
  • 高敏感应用如何保护自身不被逆向?iOS 安全加固策略与工具组合实战(含 Ipa Guard 等)
  • wxpython快捷键示例
  • SpringBoot3整合MySQL8的注意事项
  • 云服务器自带的防御可靠吗
  • 使用 minicom 录制串口报文并回放
  • 数据融合是什么?进行数据融合的4大关键环节!
  • AI开启光伏新时代:精准计算每小时发电量​
  • 【JS进阶】ES5 实现继承的几种方式
  • 微软认证考试科目众多?该如何选择?