当前位置:网站首页>el-dialog拖拽,边界问题完全修正,网上版本的bug修复
el-dialog拖拽,边界问题完全修正,网上版本的bug修复
2022-06-26 15:16:00 【hikktn】
前言
欢迎大家来到我的博客,请各位看客们点赞、收藏、关注三连!
欢迎大家关注我的知识库,Java之从零开始·语雀
你的关注就是我前进的动力!
CSDN专注于问题解决的博客记录,语雀专注于知识的收集与汇总,包括分享前沿技术。
需求
可拖拽、边界问题。
修改点
原因

看图说话,这个offsetHeight边距是盒子的距离,可是我们把这个盒子计算进去了,多算了高度,致使弹出框跑出了页面。
编写基础js,取名叫drag.js
export default {
bind(el, binding, vnode) {
const dialogHeaderEl = el.querySelector('.el-dialog__header')
const dragDom = el.querySelector('.el-dialog')
dialogHeaderEl.style.cursor = 'move'
dragDom.style.cssText += ';top:0px;'
// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
const getStyle = (function() {
if (window.document.currentStyle) {
return (dom, attr) => dom.currentStyle[attr]
} else {
return (dom, attr) => getComputedStyle(dom, false)[attr]
}
})()
dialogHeaderEl.onmousedown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX - dialogHeaderEl.offsetLeft
const disY = e.clientY - dialogHeaderEl.offsetTop
const screenWidth = document.body.clientWidth
const screenHeight = document.body.clientHeight
const minDragDomLeft = dragDom.offsetLeft
const maxDragDomLeft = screenWidth - dragDom.offsetLeft
const minDragDomTop = dragDom.offsetTop
const maxDragDomTop = screenHeight - dragDom.offsetTop
// 获取到的值带px 正则匹配替换
let styL = getStyle(dragDom, 'left')
let styT = getStyle(dragDom, 'top')
if (styL.includes('%')) {
styL = +document.body.clientWidth * (+styL.replace(/\%/g, '') / 100)
styT = +document.body.clientHeight * (+styT.replace(/\%/g, '') / 100)
} else {
styL = +styL.replace(/px/g, '')
styT = +styT.replace(/px/g, '')
}
document.onmousemove = function(e) {
// 通过事件委托,计算移动的距离
let left = e.clientX - disX
let top = e.clientY - disY
// 边界处理
if (-(left) > minDragDomLeft) {
left = -minDragDomLeft
} else if (left > maxDragDomLeft) {
left = maxDragDomLeft
}
if (-(top) > minDragDomTop) {
top = -minDragDomTop
} else if (top > maxDragDomTop) {
top = maxDragDomTop
}
// 移动当前元素
dragDom.style.cssText += `;left:${
left + styL}px;top:${
top + styT}px;`
}
document.onmouseup = function(e) {
document.onmousemove = null
document.onmouseup = null
}
}
}
}
编辑外层使用的指令文件,取名叫el-drag-dialog.js
import drag from './drag'
const install = function(Vue) {
Vue.directive('el-drag-dialog', drag)
}
if (window.Vue) {
window['el-drag-dialog'] = drag
Vue.use(install); // eslint-disable-line
}
drag.install = install
export default drag
在vue组件中使用
<el-dialog v-el-drag-dialog></el-dialog>
import elDragDialog from '@/directive/el-drag-dialog'
directives: {
elDragDialog },
参考链接:https://blog.csdn.net/weixin_46074961/article/details/106905396
边栏推荐
- 【TcaplusDB知识库】TcaplusDB系统管理介绍
- Execution of commands in the cluster
- 北京房山区专精特新小巨人企业认定条件,补贴50万
- Cache page keepalive use in Vue
- 【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
- 面试高频 | 你追我赶的Flink双流join
- Evaluation - TOPSIS
- RestCloud ETL抽取動態庫錶數據實踐
- RestCloud ETL解决shell脚本参数化
- Is it safe to open a stock account through the account opening link of the broker manager? Or is it safe to open an account in a securities company?
猜你喜欢
Advanced operation of MySQL database basic SQL statement tutorial

RestCloud ETL抽取动态库表数据实践

Vsomeip3 dual computer communication file configuration

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
![[C language practice - printing hollow upper triangle and its deformation]](/img/56/6a88b3d8de32a3215399f915bba33e.png)
[C language practice - printing hollow upper triangle and its deformation]

【微信小程序】事件绑定,你搞懂了吗?

【ceph】CephFS 内部实现(三):快照

【SNMP】snmp trap 介绍、安装、命令|Trap的发送与接收代码实现

数据库-序列

使用RestCloud ETL Shell组件实现定时调度DataX离线任务
随机推荐
About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
【TcaplusDB知识库】TcaplusDB运维单据介绍
Comparative analysis of restcloud ETL and kettle
RestCloud ETL抽取動態庫錶數據實踐
Sikuli 基于图形识别的自动化测试技术
[applet practice series] Introduction to the registration life cycle of the applet framework page
粒子滤波 PF——在机动目标跟踪中的应用(粒子滤波VS扩展卡尔曼滤波)
Restcloud ETL extraction de données de table de base de données dynamique
One click GCC script installation
Unity unitywebrequest download package
Lexin AWS IOT expresslink module achieves universal availability
Redis-集群
RestCloud ETL解决shell脚本参数化
ETL过程中数据精度不准确问题
有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
JS handwritten bind, apply, call
数据库-完整性约束
Deployment of kubernetes' controller
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
Applicable and inapplicable scenarios of mongodb series