1.在项目的详情页面,通过点击事件会弹出一个弹窗,弹窗的内容需要根据详情页面的内容进行填写,内容会被弹窗遮盖,为了提升用于体验,就需要弹窗的拖动(element-ui不支持弹窗拖动,plus支持)
2.效果图,开始时只给弹框的头部加了可以移动的事件,但是在拖动弹框的时候有可能会把头部遮盖,所以在弹框的尾部也加了移动事件,可能保证无论什么时候都可以拖动弹框
3.主要代码如下
// 在until文件夹下新建文件directives.js,内容如下
import Vue from 'vue'
// v-dialogDrag: 弹窗拖拽
Vue.directive('dialogDrag', {
bind (el, binding, vnode, oldVnode) {
const dialogHeaderEl = el.querySelector('.el-dialog__header') // 头部
const dialogfootEl = el.querySelector('.el-dialog__footer') // 尾部
const dragDom = el.querySelector('.el-dialog')
dialogHeaderEl.style.cursor = 'move'
dialogfootEl.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 () {
document.onmousemove = null
document.onmouseup = null
}
},
// 尾部绑定事件
dialogfootEl.onmousedown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX - dialogfootEl.offsetLeft
const disY = e.clientY
console.log(e.clientX, dialogfootEl.offsetLeft)
console.log(e.clientY, dialogfootEl.offsetTop)
console.log(disX, disY)
// 获取到的值带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
}
}
}
})
4.在main.js里面引入文件
import './utils/directives.js' // 为弹窗移动
5.使用自定义指令
<template>
<div>
<el-button type="primary" @click="open"> 打开弹窗 </el-button>
<el-dialog
:visible.sync="dialogVisible"
title="选择商品"
width="800px"
top="2vh"
:show-close="false"
append-to-body
class="no-padding-dialog upload-dialog"
v-if="dialogVisible"
:modal-append-to-body='false'
、、、指令的使用、、、
v-dialogDrag
:close-on-click-modal="false"
>
<!-- 弹框内容 -->
<!-- <good-select ref="select" v-if="dialogVisible" /> -->
<span slot="footer" class="dialog-footer">
<!-- 需要阻止默认事件,貌似360浏览器会优先识别移动事件,导致点击事件不生效 -->
<el-button @click.stop.prevent="close">取 消</el-button>
<el-button type="primary" @click.stop.prevent="sure">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data () {
return {
dialogVisible: false,
}
},
methods: {
open () {
this.dialogVisible = true
},
close () {
this.dialogVisible = false
},
sure () {
this.dialogVisible = false
}
}
}
</script>
<style lang="scss" scoped>
</style>
6.如果是vue3+elementplus就不需要这么麻烦,组件有提供移动的方法