dragWidth.js 1.2 KB

1234567891011121314151617181920212223242526272829
  1. /**
  2. * v-dialogDragWidth 可拖动弹窗宽度(右侧边)
  3. */
  4. export default {
  5. bind(el) {
  6. const dragDom = el.querySelector('.el-dialog');
  7. const lineEl = document.createElement('div');
  8. lineEl.style = 'width: 5px; background: inherit; height: 80%; position: absolute; right: 0; top: 0; bottom: 0; margin: auto; z-index: 1; cursor: w-resize;';
  9. lineEl.addEventListener('mousedown',
  10. function (e) {
  11. // 鼠标按下,计算当前元素距离可视区的距离
  12. const disX = e.clientX - el.offsetLeft;
  13. // 当前宽度
  14. const curWidth = dragDom.offsetWidth;
  15. document.onmousemove = function (e) {
  16. e.preventDefault(); // 移动时禁用默认事件
  17. // 通过事件委托,计算移动的距离
  18. const l = e.clientX - disX;
  19. dragDom.style.width = `${curWidth + l}px`;
  20. };
  21. document.onmouseup = function (e) {
  22. document.onmousemove = null;
  23. document.onmouseup = null;
  24. };
  25. }, false);
  26. dragDom.appendChild(lineEl);
  27. }
  28. }