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

vue2.0实现拖动div使得上下div高度变化

  <template><div class="resizable-container"><!-- 上半部分内容区 --><div class="resizable-top":style="{ height: topHeight + 'px' }">上半部分内容(可拖动下方分隔线调整高度)</div><!-- 拖动分隔线 --><div class="resizable-handle"@mousedown="startDrag":style="{ cursor: dragStatus ? 'row-resize' : 'row-resize' }"></div><!-- 下半部分内容区 --><div class="resizable-bottom":style="{ height: bottomHeight + 'px' }">下半部分内容</div></div></template><script>export default {name: "ResizableDiv",data() {return {// 容器总高度(可根据需求调整,也可动态获取父容器高度)containerHeight: 600,// 上半部分高度(初始值)topHeight: 300,// 下半部分高度(由总高度 - 上半部分高度 - 分隔线高度计算)bottomHeight: 298,// 拖动状态标记dragStatus: false,// 分隔线高度(固定)handleHeight: 4,// 上下部分最小高度限制minTopHeight: 100,minBottomHeight: 100startY:0,startTopHeight:0,};},mounted() {// 可选:动态获取父容器高度(替代固定 containerHeight)// this.containerHeight = this.$el.offsetHeight;// this.calcBottomHeight();// 监听全局鼠标移动和松开事件(避免拖动时鼠标移出分隔线失效)window.addEventListener("mousemove", this.onDrag);window.addEventListener("mouseup", this.endDrag);window.addEventListener("mouseleave", this.endDrag);},beforeDestroy() {// 移除全局事件监听(防止内存泄漏)window.removeEventListener("mousemove", this.onDrag);window.removeEventListener("mouseup", this.endDrag);window.removeEventListener("mouseleave", this.endDrag);},methods: {// 开始拖动:记录初始状态startDrag(e) {this.dragStatus = true;// 记录鼠标初始 Y 坐标(相对于容器顶部)this.startY = e.clientY - this.$el.getBoundingClientRect().top;// 记录拖动前的上半部分高度this.startTopHeight = this.topHeight;// 添加拖动时的全局样式(优化体验)document.body.style.userSelect = "none"; // 禁止文本选中document.body.style.cursor = "row-resize";},// 拖动中:计算高度变化onDrag(e) {if (!this.dragStatus) return;// 计算鼠标移动后的 Y 坐标(相对于容器顶部)const currentY = e.clientY - this.$el.getBoundingClientRect().top;// 计算上半部分高度变化值const diff = currentY - this.startY;let newTopHeight = this.startTopHeight + diff;// 边界控制:确保上半部分不小于最小高度,且下半部分也不小于最小高度const maxTopHeight = this.containerHeight - this.minBottomHeight - this.handleHeight;newTopHeight = Math.max(this.minTopHeight, Math.min(newTopHeight, maxTopHeight));// 更新上下部分高度this.topHeight = newTopHeight;this.calcBottomHeight();},// 结束拖动:重置状态endDrag() {this.dragStatus = false;// 恢复全局样式document.body.style.userSelect = "";document.body.style.cursor = "";},// 计算下半部分高度(总高度 - 上半部分高度 - 分隔线高度)calcBottomHeight() {this.bottomHeight = this.containerHeight - this.topHeight - this.handleHeight;}}};</script><style scoped>.resizable-container {width: 100%;height: 100%; /* 可根据父容器调整,或使用固定高度 */max-height: 800px;border: 1px solid #e8e8e8;border-radius: 4px;overflow: hidden;position: relative;}/* 上半部分内容区 */.resizable-top {width: 100%;overflow: auto; /* 内容超出时滚动 */background: #fafafa;padding: 16px;box-sizing: border-box;transition: height 0.1s ease; /* 平滑过渡 */}/* 拖动分隔线 */.resizable-handle {width: 100%;height: 4px;background: #ddd;cursor: row-resize;transition: background 0.2s;}/* 拖动时分隔线样式优化 */.resizable-handle:hover,.resizable-handle:active {background: #1890ff; /* Element UI 主题色,可自定义 */}/* 下半部分内容区 */.resizable-bottom {width: 100%;overflow: auto; /* 内容超出时滚动 */background: #fff;padding: 16px;box-sizing: border-box;transition: height 0.1s ease; /* 平滑过渡 */}</style>
http://www.jsqmd.com/news/40059/

相关文章:

  • 从日均 300 次攻击到零报警:外贸 SOHO 用雷池 WAF 护住独立站,Apache 环境 3 步部署太香了
  • AMBA CHI CI-700:移动SoC缓存一致性互连的核心解决方案 - ENGINEER
  • QCS8550 大模型性能深度解析
  • 当下国内商标知识产权咨询专业推荐
  • 2025 最新成都全屋定制公司推荐排行榜:武侯区 / 新房 / 装修设计优质商家权威榜单成都全屋定制方案设计公司推荐
  • 2025年水煤气钢管十大品牌权威排名:江苏华力钢管领跑行业
  • dotnet-sdk-10.0.100-win-x64.exe 怎么安装?Win10/Win11 安装步骤教程
  • 使用CDN后如何更新同名文件
  • 2025上海出国留学机构有哪几家
  • 2025上海出国留学机构一共有几家公司啊知乎
  • 2025上海出国留学机构推荐有哪些地方
  • 2025上海出国留学机构推荐有哪些
  • windows到linux服务器文件工具
  • ABPVNext项目中使用HangFire定时任务(支持租户模式) - 梦想代码
  • 巧用异步监听切面,提高系统性能
  • 2025年镜面电火花加工油直销厂家权威推荐榜单:清洗剂/水溶性切削液/不锈钢攻牙油源头厂家精选
  • 深入解析:【金仓数据库产品体验官】实战测评:电科金仓数据库接口兼容性深度体验
  • 使用AI对话和编程的一些提示词和用法
  • 实现“拼好库”,让你的 NuGet 包同时支持库调用和源生成器解析
  • 2025年膜结构工程订做厂家权威推荐榜单:膜结构遮阳棚/膜结构汽车棚/膜结构景观棚源头厂家精选
  • [忘发了]P3710 方方方的数据结构
  • open-type=chooseAvatar
  • 2025年防洪松木桩批发厂家权威推荐榜单:河道木桩/6米松木桩/人工湖木桩源头厂家精选
  • 2025年口碑好的实木楼梯定制:十大品牌综合评测与选择指南
  • 仿手绘画流程图工具 excalidraw
  • 2025 养老保险规划公司最新推荐榜:国际测评认证优质企业,综合实力与服务竞争力深度解析
  • Java CountDownLatch
  • GEO:AI搜索时代的新增长方式,以及灵捷AI的实践路径
  • 详细介绍:JVM Java虚拟机
  • [电调]AM32电调调参系列 —— Active brake on stop power 和 Brake on stop的区别