Ant-design-vue: 什么时候modal可以支持拖动呀?

Created on 10 Apr 2020  ·  3Comments  ·  Source: vueComponent/ant-design-vue

  • [ ] I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

官方有没有打算针对modal支持可拖动功能啊,目前有一些开源的可拖动的都是用React写的. 咱们vue版本的有木有可拖动的modal?

What does the proposed API look like?

希望能够在a-modal组件中添加一个配置项,是否可拖动

Most helpful comment

<!--
 * @description: 扩展modal拖拽事件
 * @author: wenbin.chen
 * @Date: 2019-05-23 09:12:18
 * @LastEditors: wenbin.chen
 * @LastEditTime: 2020-05-22 17:23:35
 * @email: [email protected]
 -->
<script>
import { Modal } from 'ant-design-vue';
export default {
  globName: 'AModal',
  mixins: [Modal],
  props: {
    // 是否开启拖拽事件
    draggable: {
      type: Boolean,
      default: true
    },
    centered: {
      type: Boolean,
      default: true
    },
    cancelText: {
      type: String,
      default: '关闭'
    },
    okText: {
      type: String,
      default: '保存'
    }
  },
  watch: {
    visible: {
      async handler(visible) {
        this.$nextTick();
        setTimeout(() => {
          this.handleModalDrag(visible);
        }, 30);
      }
    }
  },
  methods: {
    handleModalDrag(visible) {
      if (visible) {
        const dragWraps = document.querySelectorAll('.ant-modal-wrap');
        for (const wrap of dragWraps) {
          const display = this.getStyle(wrap, 'display');
          const draggable = wrap.getAttribute('data-drag');
          if (display !== 'none') {
            // 拖拽位置
            draggable === null && this.drag(wrap);
          }
        }
      }
    },
    getStyle(dom, attr) {
      if (window.document.currentStyle) {
        return dom.currentStyle[attr];
      } else {
        return getComputedStyle(dom, false)[attr];
      }
    },
    drag(wrap) {
      if (!wrap) return;
      wrap.setAttribute('data-drag', this.draggable);
      const dialogHeaderEl = wrap.querySelector('.ant-modal-header');
      const dragDom = wrap.querySelector('.ant-modal');

      if (!dialogHeaderEl || !dragDom || !this.draggable) return;

      dialogHeaderEl.style.cursor = 'move';
      // const sty =
      //   dragDom.currentStyle || window.getComputedStyle(dragDom, null);
      dialogHeaderEl.onmousedown = (e) => {
        // 鼠标按下,计算当前元素距离可视区的距离
        const disX = e.clientX;
        const disY = e.clientY;
        const screenWidth = document.body.clientWidth; // body当前宽度
        const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)

        const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
        const dragDomheight = dragDom.offsetHeight; // 对话框高度

        const minDragDomLeft = dragDom.offsetLeft;

        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
        const minDragDomTop = dragDom.offsetTop;
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
        // 获取到的值带px 正则匹配替换
        const domLeft = this.getStyle(dragDom, 'left');
        const domTop = this.getStyle(dragDom, 'top');
        let styL = domLeft;
        let styT = domTop;

        // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
        if (domLeft.includes('%')) {
          styL =
            +document.body.clientWidth * (+domLeft.replace(/%/g, '') / 100);
          styT =
            +document.body.clientHeight * (+domTop.replace(/%/g, '') / 100);
        } else {
          styL = +domLeft.replace(/px/g, '');
          styT = +domTop.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;`;
        };

        document.onmouseup = function(e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      };
    }
  }
};
</script>

All 3 comments

支持!拖动有的时候还是很有必要的

<!--
 * @description: 扩展modal拖拽事件
 * @author: wenbin.chen
 * @Date: 2019-05-23 09:12:18
 * @LastEditors: wenbin.chen
 * @LastEditTime: 2020-05-22 17:23:35
 * @email: [email protected]
 -->
<script>
import { Modal } from 'ant-design-vue';
export default {
  globName: 'AModal',
  mixins: [Modal],
  props: {
    // 是否开启拖拽事件
    draggable: {
      type: Boolean,
      default: true
    },
    centered: {
      type: Boolean,
      default: true
    },
    cancelText: {
      type: String,
      default: '关闭'
    },
    okText: {
      type: String,
      default: '保存'
    }
  },
  watch: {
    visible: {
      async handler(visible) {
        this.$nextTick();
        setTimeout(() => {
          this.handleModalDrag(visible);
        }, 30);
      }
    }
  },
  methods: {
    handleModalDrag(visible) {
      if (visible) {
        const dragWraps = document.querySelectorAll('.ant-modal-wrap');
        for (const wrap of dragWraps) {
          const display = this.getStyle(wrap, 'display');
          const draggable = wrap.getAttribute('data-drag');
          if (display !== 'none') {
            // 拖拽位置
            draggable === null && this.drag(wrap);
          }
        }
      }
    },
    getStyle(dom, attr) {
      if (window.document.currentStyle) {
        return dom.currentStyle[attr];
      } else {
        return getComputedStyle(dom, false)[attr];
      }
    },
    drag(wrap) {
      if (!wrap) return;
      wrap.setAttribute('data-drag', this.draggable);
      const dialogHeaderEl = wrap.querySelector('.ant-modal-header');
      const dragDom = wrap.querySelector('.ant-modal');

      if (!dialogHeaderEl || !dragDom || !this.draggable) return;

      dialogHeaderEl.style.cursor = 'move';
      // const sty =
      //   dragDom.currentStyle || window.getComputedStyle(dragDom, null);
      dialogHeaderEl.onmousedown = (e) => {
        // 鼠标按下,计算当前元素距离可视区的距离
        const disX = e.clientX;
        const disY = e.clientY;
        const screenWidth = document.body.clientWidth; // body当前宽度
        const screenHeight = document.documentElement.clientHeight; // 可见区域高度(应为body高度,可某些环境下无法获取)

        const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
        const dragDomheight = dragDom.offsetHeight; // 对话框高度

        const minDragDomLeft = dragDom.offsetLeft;

        const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
        const minDragDomTop = dragDom.offsetTop;
        const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight;
        // 获取到的值带px 正则匹配替换
        const domLeft = this.getStyle(dragDom, 'left');
        const domTop = this.getStyle(dragDom, 'top');
        let styL = domLeft;
        let styT = domTop;

        // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
        if (domLeft.includes('%')) {
          styL =
            +document.body.clientWidth * (+domLeft.replace(/%/g, '') / 100);
          styT =
            +document.body.clientHeight * (+domTop.replace(/%/g, '') / 100);
        } else {
          styL = +domLeft.replace(/px/g, '');
          styT = +domTop.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;`;
        };

        document.onmouseup = function(e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
      };
    }
  }
};
</script>

@jave-joe @MorningGlow 可以自己扩展实现

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ankology picture ankology  ·  3Comments

YingZzz picture YingZzz  ·  4Comments

YiXingYuFeng picture YiXingYuFeng  ·  3Comments

AhJane picture AhJane  ·  3Comments

oleksii-shaposhnikov picture oleksii-shaposhnikov  ·  4Comments