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

自定义拖拽功能,上下拖拽改变盒子高度

在这里插入图片描述
核心在于监听鼠标的move来改变div的高度,抽成了组件

<template><div ref="container" class="drag"><z-tooltip v-if="isShowIcon" effect="dark" content="格式化" placement="top-start"><div @click="handleClick"><c-icon icon-class="TextFormatwenbengeshi10" class-name="code-icon"></c-icon></div></z-tooltip><div class="bottom" :style="{ height: bottomHeight + 'px' }"><slot name="content"></slot></div><div ref="top" v-draggable class="top"></div></div>
</template><script>
export default {name: 'Drag',directives: {draggable: {inserted(el) {el.style.cursor = 'ns-resize'}}},props: {minHeight: {type: Number,default: 85},maxHeight: {type: Number,default: 600},height: {type: Number,default: 85},isShowIcon: {type: Boolean,default: false}},data() {return {bottomHeight: 85 // 初始化底部div的高度}},watch: {height: {handler(val) {this.bottomHeight = valthis.instance?.layout()},immediate: true,deep: true}},mounted() {this.dragControllerDiv()},methods: {dragControllerDiv() {const containerDiv = this.$refs.containerconst topDiv = this.$refs.toplet startMouseYlet startContainerHeightconst mouseMoveHandler = e => {e.preventDefault()// 计算拖动距离const deltaY = e.clientY - startMouseY// 计算新的容器高度const newContainerHeight = startContainerHeight + deltaY// 高度限制const clampedHeight = Math.max(this.minHeight, Math.min(this.maxHeight, newContainerHeight))// 计算底部div的高度const newBottomHeight = clampedHeight - topDiv.offsetHeightthis.$emit('move', newBottomHeight)// 更新底部div的高度this.bottomHeight = newBottomHeight}const mouseUpHandler = () => {document.removeEventListener('mousemove', mouseMoveHandler)document.removeEventListener('mouseup', mouseUpHandler)}topDiv.addEventListener('mousedown', e => {e.preventDefault()startMouseY = e.clientYstartContainerHeight = containerDiv.offsetHeight + 10document.addEventListener('mousemove', mouseMoveHandler)document.addEventListener('mouseup', mouseUpHandler)})},handleClick() {console.log('121')this.$emit('up')}}
}
</script><style>
/* 拖拽相关样式 */
.drag {position: relative;width: 100%;
}.top {position: absolute;bottom: 0;height: 2px;cursor: ns-resize;background-color: rgb(187 187 187);z-index: 100;width: calc(100% - 10px);left: 11px;&:hover {background-color: #3693ff;height: 3px;}
}.bottom {height: auto;background-color: #fff;z-index: 100;width: 100%;overflow: hidden;
}
.code-icon {float: right;cursor: pointer;
}
</style>
http://www.lryc.cn/news/138522.html

相关文章:

  • JavaScript Es6_4笔记
  • Python“牵手”易贝(Ebay)商品列表数据,关键词搜索ebayAPI接口数据,ebayAPI接口申请指南
  • C语言:选择+编程(每日一练Day8)
  • 使用 uniapp 适用于wx小程序 - 实现移动端头部的封装和调用
  • ARM Linux 系统稳定性分析入门及渐进 13 -- gdb 反汇编 disassemble 命令详细介绍及举例】
  • python连接Microsoft SQL Server 数据库
  • docker可视化工具
  • MySQL 用户管理操作
  • 【python办公自动化】PysimpleGUI中的popup弹窗中的按钮设置居中
  • postgresql 同步流复制两个相关参数synchronous_commit 和 synchronous_standby_names
  • 运算放大器发展史
  • LVS+Keepalived 实验
  • FreeSWITCH 1.10.10 简单图形化界面1 - docker/脚本/ISO镜像安装
  • 内网渗透神器CobaltStrike之权限提升(七)
  • 使用haproxy搭建web架构
  • Java基础之IO流File类创建及删除
  • 高速道路监控:工业路由器助力高速监控远程管理与维护
  • 【校招VIP】前端基础之post和get
  • 如何合理设计API接口?
  • Jsp 解决out.print()输出多出空行
  • SMC状态机 讲解2 从模型到SMC
  • MyBatis-Plus的使用
  • 板卡设计+硬件每日学习十个知识点(44)23.8.24 (检测单元设计,接口部分设计,板卡电源输入设计,电源检测电路)
  • jmeter HTTP信息头管理器
  • 各种中间件的默认端口
  • leetcode303. 区域和检索 - 数组不可变(java)
  • PHP 安装Composer,vue前端依赖包
  • OpenCV项目开发实战--基于Python/C++实现鼠标注释图像和轨迹栏来控制图像大小
  • ❤ Vue使用Eslint检测报错问题和解决
  • 解决运行在微信小程序中报[ app.json 文件内容错误] app.json: app.json 未找到(env: Windows,mp,1.05.2204