修改ElementUI DIialog弹窗样式,并可拖动
2022-02-28 本文已影响0人
挣扎在黑暗中的码畜
实现效果:
image.png
<!--基础弹窗组件-->
<template>
<div class="dialog">
<el-dialog v-dialogDrag
top="15%"
title=""
:show-close="true"
:modal="false"
append-to-body
destroy-on-close
:fullscreen="fullscreen"
:visible.sync="visible"
:width="width">
<div>这是一段信息</div>
<div>这是一段信息</div>
<div>这是一段信息</div>
<div>这是一段信息</div>
<div>这是一段信息</div>
<div slot="footer" class="dialog-footer">
</div>
</el-dialog>
</div>
</template>
<script>
export default {
props: {
info: {
type: Object,
default: () => {}
}
},
components: {},
data() {
return {
top:'15%',//margin-top
width:'30%',//弹窗宽度
fullscreen: false,//是否为全屏
visible: true,//是否显示
}
},
methods: {
}
}
</script>
<style lang="scss">
.el-dialog {
background: transparent !important;
box-shadow: 0 0px 0px rgb(0 0 0 / 0%);
.el-dialog__headerbtn {
top: 5px !important;
right: 12px !important;
background: url('~@/assets/images/dialog/bg-500-close.png') left no-repeat;
background-size: contain;
}
.el-dialog__headerbtn i {
font-size: 17px;
visibility: hidden;
}
.el-dialog__header {
background-image: url("~@/assets/images/dialog/bg-500-1.png");
}
.el-dialog__body {
background-image: url("~@/assets/images/dialog/bg-500-2.png");
}
.el-dialog__footer{
background-image: url("~@/assets/images/dialog/bg-500-3.png");
}
}
</style>
实现可拖动:
新建dialog.js ,在main.js中引入,在<el-dialog 标签增加 v-dialogDrag
//main.js
import './utils/dialog'
//dialog.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
}
}
}
})