web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

el-dialog定义了一个参数实现弹窗可拖动

396 ℃
     

功能介绍:element组件el-dialog弹窗没有找到可以拖动的功能,下面web建站小编给大家一个方法。

1、新建一个dialogDrag.js

import Vue from 'vue'
 
 // v-dialogDrag: 弹窗拖拽
 Vue.directive('dialogDrag', {
   bind(el, binding, vnode, oldVnode) {
     const dialogHeaderEl = el.querySelector('.el-dialog__header')
     const dragDom = el.querySelector('.el-dialog')
     dialogHeaderEl.style.cursor = 'move'
 
     // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
     const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
 
     dialogHeaderEl.onmousedown = (e) => {
       // 鼠标按下,计算当前元素距离可视区的距离
       const disX = e.clientX - dialogHeaderEl.offsetLeft
       const disY = e.clientY - dialogHeaderEl.offsetTop
 
       // 获取到的值带px 正则匹配替换
       let styL, styT
 
       // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
       if (sty.left.includes('%')) {
         styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
         styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
       } else {
         styL = +sty.left.replace(/px/g, '')
         styT = +sty.top.replace(/px/g, '')
       }
 
       document.onmousemove = function(e) {
         // 通过事件委托,计算移动的距离
         const l = e.clientX - disX
         const t = e.clientY - disY
 
         // 移动当前元素
         dragDom.style.left = `${l + styL}px`
         dragDom.style.top = `${t + styT}px`
 
         // 将此时的位置传出去
         // binding.value({x:e.pageX,y:e.pageY})
       }
 
       document.onmouseup = function(e) {
         document.onmousemove = null
         document.onmouseup = null
       }
     }
   }
 })
 
 // v-dialogDragWidth: 弹窗宽度拖大 拖小
 Vue.directive('dialogDragWidth', {
   bind(el, binding, vnode, oldVnode) {
     const dragDom = binding.value.$el.querySelector('.el-dialog')
 
     el.onmousedown = (e) => {
       // 鼠标按下,计算当前元素距离可视区的距离
       const disX = e.clientX - el.offsetLeft
 
       document.onmousemove = function(e) {
         e.preventDefault() // 移动时禁用默认事件
 
         // 通过事件委托,计算移动的距离
         const l = e.clientX - disX
         dragDom.style.width = `${l}px`
       }
 
       document.onmouseup = function(e) {
         document.onmousemove = null
         document.onmouseup = null
       }
     }
   }
 })

2、main.js引入

import '**/dialogDrag.js'

3、在el-dialog引入v-dialogDrag

<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogVisible"
    title="新增">
  </el-dialog>
</template>

el-dialog中获取el-table组件的ref值报undefined错误!

element-ui如何去掉el-dialog遮罩层并且弹窗下面可以编辑

el-dialog弹窗全局垂直居中设置

标签: el-dialog

上面是“el-dialog定义了一个参数实现弹窗可拖动”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_2280.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > vuejs
本文共计1866个字,预计阅读时长13分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款免费可商用仿宋字体——汉字之美仿宋GBK
下一篇: 推荐一款免费可商用英文字体——Atkinson Hyperlegible
x 打工人ai神器