• 为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

  • 相关阅读:
    记录一次开源 MaxKey 安装部署
    ASAN地址消毒+GCOV覆盖率分析
    博客积分上一万了
    CSP-J-2004-花生采摘
    k8s中使用prometheus operator监控外部服务器部署的windows exporter
    TikTok创意大赛:如何制作病毒性视频
    java毕业设计病人追踪治疗信息系统mybatis+源码+调试部署+系统+数据库+lw
    【VIM TMUX】开发工具 Vim 在 bash 中的显示与 tmux 中的显示不同
    大数据在线实习项目能收获什么呢?
    ESP8266-Arduino网络编程实例-ESP-MESH传感器数据发送与接收
  • 原文地址:https://blog.csdn.net/carcarrot/article/details/133749339