elementui中el-dialog弹窗移动缩放大小(保留上次位置大小)

demo地址链接

效果演示

在这里插入图片描述

一、创建一个dialog.js文件,并在main.js中进行引入(dialog.js路径位置随个人习惯即可)

import Vue from 'vue'

// v-dialogDrag: 弹窗拖拽属性
Vue.directive('dialogDrag', {
  bind (el, binding, vnode, oldVnode) {
    // 自定义属性,判断是否可拖拽
    if (!binding.value) return
    const dialogHeaderEl = el.querySelector('.el-dialog__header')
    const dragDom = el.querySelector('.el-dialog')
    dialogHeaderEl.style.cssText += ';cursor:move;'
    dragDom.style.cssText += ';top:0px;'

    // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    const sty = (function () {
      if (document.body.currentStyle) {
        // 在ie下兼容写法
        return (dom, attr) => dom.currentStyle[attr]
      } else {
        return (dom, attr) => getComputedStyle(dom, false)[attr]
      }
    })()

    dialogHeaderEl.onmousedown = (e) => {
      // 鼠标按下,计算当前元素距离可视区的距离
      // 鼠标距离窗口左侧距离 - 弹窗头部距离定位父元素(弹窗整体)距离,为0
      const disX = e.clientX - dialogHeaderEl.offsetLeft
      const disY = e.clientY - dialogHeaderEl.offsetTop

      // 浏览器宽高
      const screenWidth = document.body.clientWidth // body当前宽度
      const screenHeight = document.documentElement.clientHeight // 可见区域高度(应为body高度,可某些环境下无法获取)

      const dragDomWidth = dragDom.offsetWidth // 对话框宽度
      const dragDomheight = dragDom.offsetHeight // 对话框高度

      // 弹窗距离浏览器左边距离
      const minDragDomLeft = dragDom.offsetLeft
      const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth

      // 弹窗距离浏览器顶部距离
      const minDragDomTop = dragDom.offsetTop
      const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight

      // 获取到的值带px 正则匹配替换
      let styL = sty(dragDom, 'left')
      // 为兼容ie
      if (styL === 'auto') styL = '0px'
      let styT = sty(dragDom, 'top')

      // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
      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 = function (e) {
        // 通过事件委托,计算移动的距离
        // console.log(e, disY)
        let left = e.clientX - disX
        let 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
        }

        // 移动当前元素
        dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
      }

      document.onmouseup = function (e) {
        document.onmousemove = null
        document.onmouseup = null
      }
      return false
    }
  }
})

// v-dialogChange: 弹窗拉伸属性
Vue.directive('dialogChange', {
  bind (el, binding, vnode, oldVnode) {
    new Vue({}).$nextTick(() => {
      // 自定义属性,判断是否可拉伸
      if (!binding.value) return
      const dragDom = el.querySelector('.el-dialog')
      const dragDomBody = el.querySelector('.el-dialog__body')
      dragDomBody.style.overflow = 'auto'
      const minHeight = 300
      const minWidth = 400
      // 右下角设置一个图标点击拖拽缩放大小(注意:此处mouse需要与代码中的类名一致,否则会报错)
      let dragMouse = document.querySelector('.mouse')
      // 鼠标拖拽
      dragMouse.onmousedown = (e) => {
        // content区域
        const content = dragDom.parentNode.parentNode.parentNode.parentNode
        const disX = e.clientX - dragDom.offsetWidth
        const disY = e.clientY - dragDom.offsetHeight

        document.onmousemove = function (e) {
          e.preventDefault() // 移动时禁用默认事件
          // 通过事件委托,计算移动的距离e
          let width = e.clientX - disX
          let height = e.clientY - disY

          if (width > content.offsetWidth && height < content.offsetHeight) {
            if (height < minHeight) {
              height = minHeight
            }
            dragDom.style.height = `${height}px`
            // 减去头部高度
            dragDomBody.style.maxHeight = `${height - 60}px`
          } else if (width < content.offsetWidth && height > content.offsetHeight) {
            if (width < minWidth) {
              width = minWidth
            }
            dragDom.style.width = `${width}px`
          } else if (width < content.offsetWidth && height < content.offsetHeight) {
            if (height < minHeight) {
              height = minHeight
            }
            if (width < minWidth) {
              width = minWidth
            }
            dragDom.style.width = `${width}px`
            dragDom.style.height = `${height}px`
            // 减去头部高度
            dragDomBody.style.maxHeight = `${height - 60}px`
          }
        }
        document.onmouseup = function (e) {
          document.onmousemove = null
          document.onmouseup = null
        }
        return false
      }
    })
  }
})

二、易错点

在这里插入图片描述

<div class="mouse fa fa-arrows-alt"></div> // fa fa-arrows-alt为第三方图标,没有的完全可以去除或使用iconfont图标替换

// 使用别的图片或是文字替换右下角的缩放logo
<div class="mouse">
// 方案一:<img src="...."/>
// 方案二:文字内容。。。
</div>

这时按住自定义图片或文字就可以进行缩放了

在这里插入图片描述

三、完整代码使用

<template>
 <!--    添加缩放、拖动指令 v-dialogDrag:{dialogDrag}=true v-dialogChange:{dialogChange}=true     -->
 
  <el-dialog :title=title :visible.sync="visible" :center="true" :before-close="handleClose" ref="dialog__wrapper"
             v-if=visible v-dialogDrag:{dialogDrag}=true v-dialogChange:{dialogChange}=true
             :close-on-click-modal="false" class="event_dialog">
    <div class="dialog-body">
      <div class="line">
        <div slot="content">
          <div class="reportDiv" v-if="context" style="color: rgba(255, 255, 255, .7); padding: 0px 10px 15px" v-html="context"></div>
          <div v-else style="text-align: center;color: rgba(255, 255, 255, .7);padding-top: 50px">暂无内容</div>
          <!--     弹窗右下角的缩放图标     -->
          <div class="mouse fa fa-arrows-alt"></div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {getStore, setStore} from '@/utils/storage'

export default {
  data () {
    return {
      reportData: null,
      title: '',
      context: '',
      visible: false
    }
  },
  methods: {
    init (item) {
      //  初始化获取富文本内容
      this.visible = true
      this.reportData = item
      this.title = this.reportData.title
      this.context = this.reportData.context
      this.$nextTick(() => {
        // 设置弹窗大小(若无需保留上次弹窗大小,此段代码可删除)
        this.setDialogSize()
      })
    },
    setDialogSize () {
      const dragDom = document.querySelector('.event_dialog .el-dialog')
      const dragDomBody = document.querySelector('.event_dialog .el-dialog__body')
      // 初始化加载上次弹窗位置大小
      if (getStore('dialog') !== null) {
        let dialog = JSON.parse(getStore('dialog'))
        dragDom.style.left = dialog.dialogLeft
        dragDom.style.top = dialog.dialogTop
        dragDom.style.width = dialog.dialogWidth
        dragDom.style.height = dialog.dialogHeight
        dragDomBody.style.maxHeight = dialog.dialogBodyMaxHeight
      } else {
        dragDom.style.left = 0
        dragDom.style.top = 0
        dragDom.style.width = '843px'
        dragDom.style.height = '760px'
        dragDomBody.style.maxHeight = '630px'
      }
    },
    // 页面关闭保存弹窗位置及大小(无相关需求业务,也可删除)
    handleClose () {
      this.$emit('eventClose')
      // 关闭的时候缓存窗口偏移、大小
      const dragDom = document.querySelector('.event_dialog .el-dialog')
      const dragDomBody = document.querySelector('.event_dialog .el-dialog__body')
      let dialog = {
        dialogLeft: dragDom.style.left,
        dialogTop: dragDom.style.top,
        dialogWidth: dragDom.style.width,
        dialogHeight: dragDom.style.height,
        dialogBodyMaxHeight: dragDomBody.style.maxHeight
      }
      setStore('dialog', dialog)
    }
  }
}
</script>
<style lang="scss">
  .event_dialog {
    .el-dialog {
      /*height: 760px;*/
      background-color: rgba(8, 14, 50, 0.1);
      .el-dialog__header .el-dialog__title {
        font-size: 28px;
      }
      .el-dialog__body {
        padding: 0;
      }
    }
  }
</style>

<style lang="scss" scoped>
  .el-dialog__header .el-dialog__title {
    color: #00f4f5!important;
  }
  .reportDiv {
    margin-top: 40px;
    font-size: 20px;
  }
  .mouse {
    position: absolute;
    bottom: 12px;
    right: 18px;
    font-size: 20px;
    color: #f1f1f1;
  }
</style>

<think>嗯,用户想在Element UIel-dialog里再打开另一个el-dialog弹窗,也就是嵌套对话框。我需要先回想一下Element UI的文档和相关组件使用方式。Element UI的对话框通常是通过visible属性控制的,所以可能需要通过父子组件之间的状态传递来实现嵌套。 首先,用户可能需要在一个对话框的按钮点击事件中,触发第二个对话框的显示。这时候,父组件的data里应该有两个控制对话框显示的变量,比如outerVisible和innerVisible。当点击外层对话框的按钮时,将innerVisible设为true,这样内层对话框就会显示出来。 不过,用户可能遇到的问题是,直接嵌套el-dialog会导致遮罩层叠加或者z-index的问题,这样内层的对话框可能被外层的遮罩挡住。这时候可能需要调整内层对话框的append-to-body属性,让它直接附加到body上,避免被父级对话框的样式影响。同时,设置modal-append-to-body为false,这样内层对话框的遮罩不会覆盖外层,但可能需要手动调整z-index来确保显示正确。 另外,用户可能需要参考Element UI的文档,看看是否有关于嵌套对话框的示例或者注意事项。根据引用[4],提到将web-types.json文件放到element-ui文件夹下可以改善代码提示,这可能对用户配置开发环境有帮助,但和实际功能实现关系不大。不过,可能需要确保使用的Element UI版本支持嵌套对话框,或者是否有已知的问题需要规避。 还有一种情况是,用户可能尝试在打开第二个对话框时,第一个对话框关闭了。这时候需要确认visible属性的绑定是否正确,确保两个对话框的显示状态是独立控制的。比如,外层对话框的关闭事件是否会影响内层,可能需要阻止默认行为或者使用.sync修饰符来同步状态。 此外,使用事件总线(如引用[3]提到的eventBus)可能是一种解决方法,但在这个场景中可能不太必要,因为状态管理可以通过父子组件传递props和事件来实现。不过,如果对话框层级较深,可能需要更复杂的状态管理,比如Vuex,但简单情况下直接使用组件内的状态即可。 代码示例方面,用户可能需要一个基本的模板,展示如何通过按钮点击切换内外层对话框的显示状态,同时设置append-to-body和相关样式属性。此外,可能需要提醒用户注意z-index的调整,确保内层对话框显示在上层。 最后,相关问题可以引导用户了解更深入的内容,比如如何处理多层弹窗的遮罩、如何优化性能,或者如何通过编程式控制对话框。同时,可能涉及Element UI其他组件的结合使用,如表单验证在嵌套对话框中的应用。</think>### 如何在Element UIel-dialog中嵌套打开另一个el-dialog? 在Element UI中实现嵌套弹窗需注意组件层级与状态控制。以下是具体实现步骤: --- #### 1. **基础实现方法** 在父组件中定义两个对话框控制变量,通过按钮触发内层弹窗: ```vue <template> <div> <el-button @click="outerVisible = true">打开外层弹窗</el-button> <!-- 外层弹窗 --> <el-dialog :visible.sync="outerVisible" title="外层弹窗"> <el-button @click="innerVisible = true">打开内层弹窗</el-button> <!-- 内层弹窗 --> <el-dialog :visible.sync="innerVisible" title="内层弹窗" append-to-body :modal-append-to-body="false" > 内层弹窗内容 </el-dialog> </el-dialog> </div> </template> <script> export default { data() { return { outerVisible: false, innerVisible: false } } } </script> ``` **关键配置**: - `append-to-body`:强制内层弹窗插入到body末尾,避免被父级样式限制[^4] - `:modal-append-to-body="false"`:防止内层弹窗的遮罩覆盖外层弹窗 --- #### 2. **解决遮罩层覆盖问题** 若内层弹窗被遮挡,可通过CSS调整层级: ```css /* 强制提高内层弹窗的z-index */ .el-dialog__wrapper:last-child { z-index: 3000 !important; } ``` --- #### 3. **事件总线优化方案** 对于复杂嵌套场景,可使用事件总线(Event Bus)管理弹窗状态: ```javascript // event-bus.js import Vue from 'vue' export const EventBus = new Vue() // 父组件 EventBus.$on('open-inner-dialog', () => { this.innerVisible = true }) // 子组件 EventBus.$emit('open-inner-dialog') ``` 此方案适用于跨层级组件通信[^3] ---
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值