• 为element-ui对话框组件(el-dialog)添加弹窗拖拽支持


    创建dialogDrag.js

    1. /************************************************
    2. Description:
    3. el-dialog弹窗组件添加拖拽支持
    4. Dependencies:
    5. vue.js
    6. el-dialog (element-ui)
    7. ************************************************/
    8. Vue.directive('dialogDrag', {
    9. bind: function (el) {
    10. const dialogHeaderEl = el.querySelector('.el-dialog__header')
    11. const dragDom = el.querySelector('.el-dialog')
    12. dialogHeaderEl.style.cursor = 'move'
    13. // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    14. const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
    15. dialogHeaderEl.onmousedown = e => {
    16. // 鼠标按下,计算当前元素距离可视区的距离
    17. //const disX = e.clientX - dialogHeaderEl.offsetLeft
    18. //const disY = e.clientY - dialogHeaderEl.offsetTop
    19. const disX = e.clientX
    20. const disY = e.clientY
    21. // 获取到的值带px 正则匹配替换
    22. let styL, styT
    23. // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
    24. if (sty.left.includes('%')) {
    25. styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
    26. styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
    27. } else {
    28. styL = +sty.left.replace(/\px/g, '')
    29. styT = +sty.top.replace(/\px/g, '')
    30. }
    31. document.onmousemove = function (e) {
    32. // 通过事件委托,计算移动的距离
    33. const l = e.clientX - disX
    34. const t = e.clientY - disY
    35. var tempX = l + styL
    36. var tempY = t + styT
    37. // 移动当前元素
    38. dragDom.style.left = `${tempX}px`
    39. dragDom.style.top = `${tempY}px`
    40. //确保不移动超出可视范围之外
    41. //(Reference: https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect)
    42. var rect = dragDom.getBoundingClientRect()
    43. while (rect.x < 0 || rect.y < 0) {
    44. if (rect.x < 0) {
    45. tempX++
    46. }
    47. if (rect.y < 0) {
    48. tempY++
    49. }
    50. dragDom.style.left = `${tempX}px`
    51. dragDom.style.top = `${tempY}px`
    52. rect = dragDom.getBoundingClientRect()
    53. }
    54. // 将此时的位置传出去
    55. // binding.value({x:e.pageX,y:e.pageY})
    56. }
    57. document.onmouseup = function () {
    58. document.onmousemove = null
    59. document.onmouseup = null
    60. }
    61. }
    62. }
    63. })

    在需要的地方的地方引入

    1.  比如vue组件扩展JVue.js中引入

    1. import "/Components/vue/dialogDrag.js"
    2. //(以import导入到JVue.js,JVue.js需以type="module"引入)

    2.  或者在使用弹窗的页面中在引入vue后以默认的type="text/javascript"引入

      

    在需要添加拖拽功能的弹窗组件el-dialog中直接使用新添加 v-dialog-drag 属性(指令)即可

    1. <el-dialog title="提示" :visible.sync="dialogVisible" width="550px" :close-on-click-modal="false" v-dialog-drag>
    2. <el-form ......>
    3. ......
    4. el-form>
    5. <div slot="footer" class="dialog-footer center">
    6. <el-button size="small" @@click="dialogVisible = false">{{ $t("sos.btn.Cancel") }}el-button>
    7. <el-button size="small" type="primary" @@click="saveForm">{{ $t("sos.btn.Ok") }}el-button>
    8. div>
    9. el-dialog>

    参考:https://juejin.cn/post/7250639505526472741

  • 相关阅读:
    C语言之指针数组、结构体、动态内存分配
    ARM 的 的 AMBA 总线
    t-io websocket的聊天功能学习记录(一)
    分类预测 | Matlab实现CNN-BiLSTM-SAM-Attention卷积双向长短期记忆神经网络融合空间注意力机制的数据分类预测
    SAP ABAP BAPI_MATERIAL_BOM_GROUP_CREATE DEMO
    阿里云11.12重大故障原因曝光
    实现动态表单的一种思路 | 京东云技术团队
    vmware安装centos7
    神经网络模型的训练过程,怎么训练神经网络模型
    荧光染料Cy7 酰肼,Cy7 hydrazide,Cy7 HZ参数及结构式解析
  • 原文地址:https://blog.csdn.net/carcarrot/article/details/133749339