vue+el-dialog 페이지 인용 드래그 창 구성 요소

16732 단어 vue
하위 페이지:사용자 지정 명령 끌기 v-el-drag-dialog directives: {elDragDialog}
khDetail.vue

	      

import elDragDialog from "@/assets/js/el-drag-dialog";

export default {
  data() {
    return {
      modalShow: false
    };
  },
  directives: { elDragDialog },
  methods: {
    init() {
      this.modalShow = true;
    },
    modalClose() {
      this.modalShow = false;
    }
  }
};


부모 페이지: (Detail은 어셈블리)


import Detail from "@/components/control/kh/khDetail.vue";
export default {
  data() {
    return {
      isShow: false,
    };
  },
  components: {
    Detail
  },
  methods: {
    /* --------------  --------------- */
    //       
    handleDetail(index, row) {
      console.log(index, row);
      this.isShow=true,
      this.$nextTick(()=>{
        this.$refs.Detail.init();
      })
    },

  }
};


봉인된 드래그 코드
drag.js
export default {
    bind(el, binding, vnode) {
      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 getStyle = (function() {
        if (window.document.currentStyle) {
          return (dom, attr) => dom.currentStyle[attr]
        } else {
          return (dom, attr) => getComputedStyle(dom, false)[attr]
        }
      })()
  
      dialogHeaderEl.onmousedown = (e) => {
        //     ,              
        const disX = e.clientX - dialogHeaderEl.offsetLeft
        const disY = e.clientY - dialogHeaderEl.offsetTop
  
        const dragDomWidth = dragDom.offsetWidth
        const dragDomHeight = dragDom.offsetHeight
  
        const screenWidth = document.body.clientWidth
        const screenHeight = document.body.clientHeight
  
        const minDragDomLeft = dragDom.offsetLeft
        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth
  
        const minDragDomTop = dragDom.offsetTop
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomHeight
  
        //       px       
        let styL = getStyle(dragDom, 'left')
        let styT = getStyle(dragDom, '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 = function(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
          }
  
          //       
          dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`
  
          // emit onDrag event
          vnode.child.$emit('dragDialog')
        }
  
        document.onmouseup = function(e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }
    }
  }

index.js
import drag from './drag'

const install = function(Vue) {
  Vue.directive('el-drag-dialog', drag)
}

if (window.Vue) {
  window['el-drag-dialog'] = drag
  Vue.use(install); // eslint-disable-line
}

drag.install = install
export default drag


좋은 웹페이지 즐겨찾기