修改ElementUI DIialog彈窗樣式,并可拖動

實現(xiàn)效果:


image.png
<!--基礎(chǔ)彈窗組件-->
<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>

實現(xiàn)可拖動:
新建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) => {
      // 鼠標按下,計算當前元素距離可視區(qū)的距離
      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
      }
    }
  }
})

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容