指令的封装转自https://blog.csdn.net/sinat_21902709/article/details/86545444

可拖拽dialog应用于很多弹出框,所以需要作用于全局

在插件文件夹中创建一个文件dialogDrag存放公共的指令

  1. import Vue from "vue";
  2.  
  3. // v-dialogDrag: 弹窗拖拽属性
  4. Vue.directive("dialogDrag", {
  5. bind(el, binding, vnode, oldVnode) {
  6. const dialogHeaderEl = el.querySelector(".el-dialog__header");
  7. const dragDom = el.querySelector(".el-dialog");
  8. //dialogHeaderEl.style.cursor = 'move';
  9. dialogHeaderEl.style.cssText += ";cursor:move;";
  10. dragDom.style.cssText += ";top:0px;";
  11.  
  12. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
  13. const sty = (function() {
  14. if (window.document.currentStyle) {
  15. return (dom, attr) => dom.currentStyle[attr];
  16. } else {
  17. return (dom, attr) => getComputedStyle(dom, false)[attr];
  18. }
  19. })();
  20.  
  21. dialogHeaderEl.onmousedown = e => {
  22. // 鼠标按下,计算当前元素距离可视区的距离
  23. const disX = e.clientX - dialogHeaderEl.offsetLeft;
  24. const disY = e.clientY - dialogHeaderEl.offsetTop;
  25.  
  26. const screenWidth = document.body.clientWidth; // body当前宽度
  27. const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)
  28.  
  29. const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
  30. const dragDomheight = dragDom.offsetHeight; // 对话框高度
  31.  
  32. const minDragDomLeft = dragDom.offsetLeft;
  33. const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
  34.  
  35. const minDragDomTop = dragDom.offsetTop;
  36. const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
  37.  
  38. // 获取到的值带px 正则匹配替换
  39. let styL = sty(dragDom, "left");
  40. let styT = sty(dragDom, "top");
  41.  
  42. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
  43. if (styL.includes("%")) {
  44. styL = +document.body.clientWidth * (+styL.replace(/\%/g, "") / );
  45. styT = +document.body.clientHeight * (+styT.replace(/\%/g, "") / );
  46. } else {
  47. styL = +styL.replace(/\px/g, "");
  48. styT = +styT.replace(/\px/g, "");
  49. }
  50.  
  51. document.onmousemove = function(e) {
  52. // 通过事件委托,计算移动的距离
  53. let left = e.clientX - disX;
  54. let top = e.clientY - disY;
  55.  
  56. // 边界处理
  57. if (-left > minDragDomLeft) {
  58. left = -minDragDomLeft;
  59. } else if (left > maxDragDomLeft) {
  60. left = maxDragDomLeft;
  61. }
  62.  
  63. if (-top > minDragDomTop) {
  64. top = -minDragDomTop;
  65. } else if (top > maxDragDomTop) {
  66. top = maxDragDomTop;
  67. }
  68.  
  69. // 移动当前元素
  70. dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;
  71. };
  72.  
  73. document.onmouseup = function(e) {
  74. document.onmousemove = null;
  75. document.onmouseup = null;
  76. };
  77. };
  78. }
  79. });

在main.js文件中引入公共指令

  1. // 引入自定义指令
  2. import "./plugins/dialogDrag/directives";

然后直接就可以在组件中使用

  1. <!--直接使用 自定义指令 v-dialogDrag-->
  2. <template>
  3. <div id="addExpressDialog"
  4. v-show="isShowExpressDialog"
  5. v-dialogDrag>
  6. <el-dialog :visible.sync="isShowExpress"
  7. class="dialog_container"
  8. center>
  9. <div slot="title"
  10. class="dialog-title">
  11. {{title}}
  12. </div>
  13. <div class="dialog_content">
  14. 内容
  15. </div>
  16. </el-dialog>
  17.  
  18. </div>
  19. </template>
  20. <script>
  21. import Vue from 'vue'
  22. import { Dialog } from 'element-ui'
  23. Vue.use(Dialog)
  24. export default {
  25. name: 'addExpressDialog',
  26. props: {
  27. title: {
  28. type: String
  29. },
  30. isShowExpressDialog: {
  31. type: Boolean,
  32. default: false
  33. },
  34. dialogType: {
  35. type: String
  36. }
  37. },
  38.  
  39. data() {
  40. return {}
  41. },
  42. mounted() {},
  43. computed: {
  44. isShowExpress: {
  45. get() {
  46. return this.isShowExpressDialog
  47. },
  48. set(v) {
  49. this.$emit('closeExpressDialog', v)
  50. }
  51. }
  52. },
  53.  
  54. watch: {},
  55. methods: {}
  56. }
  57. </script>
  58. <style lang="scss">
  59. .v-modal {
  60. z-index: 0 !important;
  61. }
  62. #addExpressDialog {
  63. .el-dialog {
  64. width: 431px;
  65. height: 222px;
  66. position: relative;
  67. margin: 0 auto;
  68. margin-top: 0px !important;
  69. margin-bottom: 0px !important;
  70. background: url('../../../../assets/images/sysinformation/bg_popup_del.png')
  71. no-repeat;
  72. .el-dialog__header {
  73. padding: 5px 0px 0px 0px;
  74. .el-dialog__headerbtn {
  75. top: 5px;
  76. }
  77. }
  78. }
  79. }
  80. </style>
  81. <style lang="scss" scoped>
  82. #addExpressDialog {
  83. position: fixed;
  84. height: calc(100% - 80px);
  85. top: 80px;
  86. bottom: 0px;
  87. left: 0px;
  88. right: 0px;
  89. z-index: 9999 !important;
  90. .dialog_container {
  91. height: calc(100% - 80px);
  92. top: 80px !important;
  93. overflow: hidden;
  94. .dialog-title {
  95. color: rgba(255, 255, 255, 1);
  96. }
  97. }
  98. }
  99. </style>

  简单效果图

可以拖拽

可拖拽dialog的更多相关文章

  1. EasyUI, Dialog 在框架页(ifrmae)的Top页面弹出时,拖拽Dialog边缘(以改变窗口大小),UI界面被卡死的解决办法

    将Dialog的modal属性设置为true,可以解决卡死的问题(但会给用户使用体验带来影响) var par = { title: This.title, width: This.width, he ...

  2. element-ui dialog组件添加可拖拽位置 可拖拽宽高

    edge浏览器下作的gifhttp://www.lanourteam.com/%E6... 有几个点需要注意一下 每个弹窗都要有唯一dom可操作 指令可以做到 拖拽时要添加可拖拽区块 header 由 ...

  3. 使dialog可拖拽指令

    在项目开发过程中,需要支持dialog弹窗可拖拽,则需要对dialog添加指令.具体操作说明如下: (1)在用于存放指令的文件夹内,新建拖拽指令文件夹,例如命名为:el-dragDialog,如下所示 ...

  4. Vue. 之 Element dialog 拖拽

    Vue. 之 Element dialog 拖拽 默认情况下,在使用Element的Dialog模块时,弹出框是不能移动的,且 一旦点击遮罩层区域,弹框就会消失. 解决方案: 1 在 utils 中新 ...

  5. wpf图片查看器,支持鼠标滚动缩放拖拽

    最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...

  6. Javascript 事件对象进阶(二)拖拽的应用 - 登录框的拖拽

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 纯JS实现可拖拽表单

    转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 因为要用到可拖拽表单,个人要比较喜欢自己动手,不怎么喜欢在不懂实现或者原理的情况下用插件,所以查找资料实现了一个. 思路:放入:用mouse ...

  8. js实现登陆页面的拖拽功能

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>登 ...

  9. C# 图片盖章功能实现,支持拖拽-旋转-放缩-保存

    实现图片盖章功能,在图片上点击,增加“图章”小图片,可以拖拽“图章”到任意位置,也可以点击图章右下角园框,令图片跟着鼠标旋转和放缩. 操作方法:1.点击增加“图章”2.选中移动图标3.点中右下角放缩旋 ...

随机推荐

  1. rabbitmq生产者queue接收不到消息

    项目问题: 客户的UAT环境下,项目运行一段时间后,rabbitmq的生产者queue再也接收不到系统发送的消息了.因为queue接收不到消息,所以消费者无法消费数据,流程断掉了. 原因: 客户UAT ...

  2. MySQL各种类型实验

    实验一:整数 -- 测试一 create database test;-- 新建数据库,如果已经有了就不需要再创建了 USE test;-- 打开数据库 drop table if exists te ...

  3. 第10组 Beta冲刺(1/5)

    链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 tomcat的学习与实现 服务器后端部署,API接口的beta版实现 后端代码 ...

  4. bat批处理 取得当前路径 %CD%

    在DOS的批处理中,有时候需要知道当前的路径.在DOS中,有两个环境变量可以跟当前路径有关,一个是%cd%, 一个是%~dp0. 这两个变量的用法和代表的内容一般是不同的. 1. %cd% 可以用在批 ...

  5. Authenticator App 两步验证会不会造成亚马逊账号关联?

    今天听人说,因为用Authenticator App做亚马逊两步验证造成了帐号关联…… 我给大家解释一下Authenticator的实现原理,作为计算机专业科班出身的我,此次从各方面了解并经过自己亲测 ...

  6. python3.6安装 zipimport.ZipImportError: can't decompress data; zlib not available【转】

    python3.6.3 安装: .tgz cd Python- ./configure make make altinstall `make altinstall` , 报错: zipimport.Z ...

  7. 解决iis内存占用过大的问题--ZT

    解决iis内存占用过大的问题 在IIS6下,经常出现w3wp的内存占用不能及时释放,从而导致服务器响应速度很慢. 今天研究了一下,可以做以下配置: 1.在IIS中对每个网站进行单独的应用程序池配置.即 ...

  8. 支付宝即时到账交易接口C#接入方式的几个坑

    1.在官方文档中 https://docs.open.alipay.com/62/104743 可以清楚看到input_charset前面没有要求加下横杠,可是请求示例是带着的.经过实验得知,这个必须 ...

  9. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  10. [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...