VUE3实现对话框窗口拖动
文章目录前言一、创建一个指令文件二、我这里是全局引入的 以我的为例1.使用指令前言提示:在开发前端项目的时候 我们可能要用到可拖动的窗口例如:下图中的效果。一、创建一个指令文件示例:/***dragMove.js当前文件名*2021/11/24-18:54创建时间*PhpStormIDE名称*作者:蒋步国<email:15616888806@163.com>*/export defau
·
前言
提示:在开发前端项目的时候 我们可能要用到可拖动的窗口
例如:下图中的效果。
一、创建一个指令文件
示例:
/**
*dragMove.js 当前文件名
*2021/11/24-18:54 创建时间
*PhpStorm IDE名称
*作者: 蒋步国<email:15616888806@163.com>
*/
export default (app) => {
app.directive('dragMove', (el, binding) => {
// body当前宽度
const screenWidth = document.body.clientWidth
// body高度
const screenHeight = document.documentElement.clientHeight
// 拖拽按钮
const DragButton = el.querySelector(binding.value.DragButton)
DragButton.style.cssText += ';cursor:move;'
// 拖拽窗口 DragVindow
const DragVindow = el.querySelector(binding.value.DragVindow)
// 如果是自定义组件 设置窗口默认居中
if (binding.value.custom) {
const [left, top] = [screenWidth - DragVindow.offsetWidth, screenHeight - DragVindow.offsetHeight]
DragVindow.style.cssText += `;left:${left / 2}px;top:${top / 2}px;`
}
const sty = (function () {
if (window.document.currentStyle) {
return (dom, attr) => dom.currentStyle[attr]
} else {
return (dom, attr) => getComputedStyle(dom, false)[attr]
}
})()
// 按下鼠标处理事件
DragButton.onmousedown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX - DragButton.offsetLeft
const disY = e.clientY - DragButton.offsetTop
const dragDomWidth = DragVindow.offsetWidth // 对话框宽度
const dragDomheight = DragVindow.offsetHeight // 对话框高度
const minDragDomLeft = DragVindow.offsetLeft
const maxDragDomLeft = screenWidth - DragVindow.offsetLeft - dragDomWidth
const minDragDomTop = DragVindow.offsetTop
const maxDragDomTop = screenHeight - DragVindow.offsetTop - dragDomheight
let styL = sty(DragVindow, 'left')
let styT = sty(DragVindow, '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 = (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
}
// 设置当前元素
DragVindow.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
}
document.onmouseup = () => {
document.onmousemove = null
document.onmouseup = null
}
}
})
}
二、我这里是全局引入的 以我的为例
示例:
在main.js 中引入
1.使用指令
在el-dialog中使用:
// DragButton 参数是窗口内头部标题 鼠标左键安装头部标题可拖动 填写类名
// DragVindow 参数是窗口主体类名
<div v-dragMove="{DragButton:'.el-dialog__header',DragVindow:'.el-dialog'}">
<el-dialog >
//....
</el-dialog>
</d/iv>
自己创建窗口:
//custom:true 在自定义窗口时 默认居中显示
<template>
<div v-dragMove="{DragButton:'.window-header',DragVindow:'.window', custom:true}">
<div class="window">
<div class="window-header" >
按住拖拽 标题
</div>
<div class="window-body">
窗口默认
</div>
</div>
</div>
</template>
// 样式示例
<style scoped lang="scss">
.window {
width: 400px;
height: 300px;
border: 1px solid #cccccc;
position: absolute;
background: #e1eafc;
.window-header {
height: 50px;
line-height: 50px;
text-align: center;
background: #beceeb;
user-select: none;
}
}
</style>
## 如果还有其他问题请给我留言 更多推荐
已为社区贡献2条内容
所有评论(0)