vue 自定义拖拽指令

2023年1月1日12:27:32
export function addDirective(app) {


  /**
   * 拖拽指令使用方式:v-drag="[dragDom, dragHeader]",如 `<div v-drag="['.drag-container .el-dialog', '.drag-container .el-dialog__header']"></div>`
   */

  app.directive('drag', {
    mounted(el, binding) {
      if (!binding.value) return false;

      binding.instance.$nextTick(() => {
        const dragDom = document.querySelector(binding.value[0])
        const dragHeader = document.querySelector(binding.value[1])

        dragHeader.onmouseover = () => (dragHeader.style.cursor = `move`);

        function down(e, type) {
          // 鼠标按下,计算当前元素距离可视区的距离
          const disX = type === 'pc' ? e.clientX - dragHeader.offsetLeft : e.touches[0].clientX - dragHeader.offsetLeft;
          const disY = type === 'pc' ? e.clientY - dragHeader.of

  • 作者:紫微前端
  • 原文链接:https://liuhao.blog.csdn.net/article/details/125002367
    更新时间:2023年1月1日12:27:32 ,共 622 字。