文章目录

  • 前言
  • 一、创建一个指令文件
  • 二、我这里是全局引入的 以我的为例
    • 1.使用指令

前言

提示:在开发前端项目的时候 我们可能要用到可拖动的窗口
例如:下图中的效果。


一、创建一个指令文件

示例:

/***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;'// 拖拽窗口 DragVindowconst 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.offsetLeftconst disY = e.clientY - DragButton.offsetTopconst dragDomWidth = DragVindow.offsetWidth // 对话框宽度const dragDomheight = DragVindow.offsetHeight // 对话框高度const minDragDomLeft = DragVindow.offsetLeftconst maxDragDomLeft = screenWidth - DragVindow.offsetLeft - dragDomWidthconst minDragDomTop = DragVindow.offsetTopconst maxDragDomTop = screenHeight - DragVindow.offsetTop - dragDomheightlet 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 - disXlet 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 = nulldocument.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>

## 如果还有其他问题请给我留言

VUE3实现对话框窗口拖动相关推荐

  1. MFC无边框对话框实现拖动

    解决无标题栏窗口的拖动问题有两种方案,一种方案是使用常规思路来处理鼠标拖拽事件,当窗口获得WM_LBUTTONDOWN(OnLButtonDown)时,通过设置标志并调用CWnd::SetCaptur ...

  2. MFC中,如何获得对话框控件相对于父窗口(对话框窗口)的位置

    在MFC中,如何获得对话框控件相对于父窗口(对话框窗口)的位置: CRect  r; pWnd->GetWindowRect(&r); 这样获得的r是控件相对于屏幕的坐标,然后用Scre ...

  3. duilib进阶教程 -- 改进窗口拖动 (12)

    现在大家应该都知道caption="0,0,0,32",是指示标题栏区了吧,如果想要整个窗口都能拖动呢? 那直接把高度改成和窗口一样不就得了~O(∩_∩)O~ 嗯,这样是可以,比如 ...

  4. 【javaScript】原生实现窗口拖动效果

    窗口拖动 通过原生javaScript进行窗口拖动的实现 一.功能 通过javaScript实现自定义容器的拖动操作,通过拖动标题部分进行窗口的移动 二.实现思路 通过鼠标左键按下触发条件 通过eve ...

  5. windows对话框窗口DialogBox模式对话框、EndDialog、CreateDialog非模式对话框、DestroyWindow、WM_INITDIALOG

    普通窗口:自定义函数调用缺省函数 wndProc(-) { - DefWindowProc(-); } 对话框窗口:缺省函数 调用自定义函数 缺省函数(-){ - 自定义函数(-) - } 对话框原理 ...

  6. EasyUi之Dialog(对话框窗口)

    Dialog(对话框窗口) Dialog(对话框窗口)+Window(窗口)+Panel(面板) Dialog属性 Dialog事件+方法 Window属性,事件,方法 Panel属性,事件,方法 案 ...

  7. 对话框窗口过程与普通窗口过程的区别

    对话框窗口是指通过CreateDialog,DialogBox 这些API创建的窗口,其中DLGPROC与普通窗口有一些区别. HWND WINAPI CreateDialog(_In_opt_ HI ...

  8. Dialog(对话框窗口)

    该对话框是一种特殊类型的窗口,它在顶部有一个工具栏,在底部有一个按钮栏.对话框窗口右上角只有一个关闭按钮用户可以配置对话框的行为显示其他工具,如collapsible,minimizable,maxi ...

  9. 【最终版】PyQt5 自定义标题栏,实现无边框,最小化最大化关闭事件,窗口拖动移动,窗口改变大小,仿百度网盘色调美化,添加内容窗口

    [最终版]PyQt5 自定义标题栏,实现无边框,最小化最大化关闭事件,窗口拖动移动,窗口改变大小,仿百度网盘色调美化,添加内容窗口 文章目录 [最终版]PyQt5 自定义标题栏,实现无边框,最小化最大 ...

最新文章

  1. yolact_ros出坑记录
  2. 医院的“数据中心”,究竟应该怎么建?
  3. 【linux高级程序设计】(第十二章)Linux多线程编程 4
  4. qt如和调用linux底层驱动_擅长复杂硬件体系设计,多核系统设计,以及基于RTOS或者Linux,QT等进行相关底层驱动。...
  5. diy 多路监控_如何通过此DIY设置监控空气质量
  6. nebula模拟器_nebula模拟器中文金手指版本
  7. 继承redis spring_Spring 极速集成注解 Redis 实践
  8. C#图片处理之: 另存为压缩质量可自己控制的JPEG
  9. Extjs介绍及视频教程
  10. 批量翻译软件免费【2022最新版】
  11. 基于C/C++的弹出气泡框
  12. Mac 新手必看:认识 Mac 桌面
  13. 2019念念不忘,2020必有回响!!!
  14. 初学VUE.js之数据绑定
  15. HLS_2 数据精度处理
  16. C#使用iTextSharp将数据导出成PDF
  17. 基于行为模拟爆破C/S架构客户端程序的一些猜想实现
  18. html交叉轴排列,10-flex 项目交叉轴单独对齐
  19. LeetCode题解(0657):根据操作列表判断机器人是否能回到原点(Python)
  20. 2013中秋大同三日游

热门文章

  1. 数据库期末考试知识点复习(1~7章)
  2. 计算机软考高项(信息系统项目管理师)、中项(系统集成项目管理工程师),统计师中级的一些备考经验
  3. mysql在GROUP_CONCAT中拼接字符串
  4. 理解Linemod匹配算法
  5. 华三无线控制器基础调试步骤
  6. 物联网开发笔记(68)- 使用Micropython开发ESP32开发板之使用官方工具esptool烧录
  7. 华为数通HCIE面试看这个就够了系列——MPLS V*N
  8. Go-在linux系统安装配置
  9. 【读书笔记】《腾讯传》读书笔记
  10. Java项目:基于jsp+mysql+Spring+SpringMVC+mybatis的房源信息管理系统