VUE3实现对话框窗口拖动


前言

提示:在开发前端项目的时候 我们可能要用到可拖动的窗口
例如:下图中的效果。

在这里插入图片描述


一、创建一个指令文件

示例:
在这里插入图片描述

/**
 *dragMove.js     当前文件名
 *2021/11/24-18:54  创建时间
 *PhpStorm  IDE名称
 *作者:  蒋步国<email:15616888806@163.com>
 */
export default (app) => {
  app.directive('dragMove', (el, binding) => {
    // body当前宽度
    const screenWidth = document.body.clientWidth
    // body高度
    const screenHeight = document.documentElement.clientHeight
    // 拖拽按钮
    const DragButton = el.querySelector(binding.value.DragButton)
    DragButton.style.cssText += ';cursor:move;'
    // 拖拽窗口 DragVindow
    const DragVindow = el.querySelector(binding.value.DragVindow)
    // 如果是自定义组件 设置窗口默认居中
    if (binding.value.custom) {
      const [left, top] = [screenWidth - DragVindow.offsetWidth, screenHeight - DragVindow.offsetHeight]
      DragVindow.style.cssText += `;left:${left / 2}px;top:${top / 2}px;`
    }
    const sty = (function () {
      if (window.document.currentStyle) {
        return (dom, attr) => dom.currentStyle[attr]
      } else {
        return (dom, attr) => getComputedStyle(dom, false)[attr]
      }
    })()

    // 按下鼠标处理事件
    DragButton.onmousedown = (e) => {
      // 鼠标按下,计算当前元素距离可视区的距离
      const disX = e.clientX - DragButton.offsetLeft
      const disY = e.clientY - DragButton.offsetTop

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

      const minDragDomLeft = DragVindow.offsetLeft
      const maxDragDomLeft = screenWidth - DragVindow.offsetLeft - dragDomWidth

      const minDragDomTop = DragVindow.offsetTop
      const maxDragDomTop = screenHeight - DragVindow.offsetTop - dragDomheight

      let styL = sty(DragVindow, 'left')
      let styT = sty(DragVindow, 'top')
      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 = (e) => {
        // 通过事件委托,计算移动的距离
        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
        }
        // 设置当前元素
        DragVindow.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
      }
      document.onmouseup = () => {
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  })
}

二、我这里是全局引入的 以我的为例

示例:
在这里插入图片描述
在main.js 中引入
在这里插入图片描述

1.使用指令

在el-dialog中使用:

// DragButton 参数是窗口内头部标题 鼠标左键安装头部标题可拖动 填写类名
// DragVindow 参数是窗口主体类名

    <div v-dragMove="{DragButton:'.el-dialog__header',DragVindow:'.el-dialog'}">
      <el-dialog >
          //....
      </el-dialog>
    </d/iv>

自己创建窗口:

//custom:true 在自定义窗口时 默认居中显示
<template>
  <div v-dragMove="{DragButton:'.window-header',DragVindow:'.window', custom:true}">
    <div class="window">
      <div class="window-header" >
        按住拖拽 标题
      </div>
      <div class="window-body">
        窗口默认
      </div>
    </div>
  </div>
</template>
// 样式示例
<style scoped lang="scss">
.window {
  width: 400px;
  height: 300px;
  border: 1px solid #cccccc;
  position: absolute;
  background: #e1eafc;
  .window-header {
    height: 50px;
    line-height: 50px;
    text-align: center;
    background: #beceeb;
    user-select: none;
  }
}
</style>
## 如果还有其他问题请给我留言
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值