vue el-dialog实现可拖沓

盛世宏图  金牌会员 | 2024-12-18 16:47:19 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 881|帖子 881|积分 2643

el-dialog实现拖沓,每次点击度居中显示,以下贴出代码具体实现,我是可以正常拖沓并且每次度显示在中心,效果还可以,需要的可以丢上去跑跑
组件部分: 
  1. <el-dialog
  2.         :visible.sync="dialogVisible"
  3.         :close-on-click-modal="false"
  4.         custom-class="draggable-dialog"
  5.         ref="dialog"
  6.         @open="onDialogOpen"
  7.         @opened="onDialogOpened"
  8.         @close="onDialogClose"
  9.         width="646px">
  10.       <div slot="title" class="header" @mousedown="startDrag">
  11.         <span>公式设置</span>
  12.       </div>
  13.         <el-divider></el-divider>
  14.         <div >
  15.             这里写你的dialog业务代码
  16.         </div>
  17. </el-dialog>
复制代码
定义变量:

函数部分:
  1. startDrag(event) {
  2.       event.preventDefault();
  3.       this.dragging = true;
  4.       this.startX = event.clientX;
  5.       this.startY = event.clientY;
  6.       const dialogRef = this.$refs.dialog.$el;
  7.       if (!dialogRef) {
  8.         console.error('无法找到对话框引用');
  9.         return;
  10.       }
  11.       console.log('获取打开后的位置dialogRef-->',dialogRef);
  12.       console.log('获取打开后的位置dialogRef.style.left-->',dialogRef.style.left);
  13.       console.log('获取打开后的位置dialogRef.style.top-->',dialogRef.style.left);
  14.       // 获取当前对话框的位置
  15.       const style = window.getComputedStyle(dialogRef);
  16.       this.currentX = parseFloat(style.left || '0');
  17.       this.currentY = parseFloat(style.top || '0');
  18.       console.log('this.currentX---------->',this.currentX);
  19.       console.log('this.currentY---------->',this.currentY);
  20.       
  21.       document.onmousemove = this.doDrag.bind(this);
  22.       document.onmouseup = this.stopDrag.bind(this);
  23.     },
  24.     doDrag(event) {
  25.       if (!this.dragging) return;
  26.       const deltaX = event.clientX - this.startX;
  27.       const deltaY = event.clientY - this.startY;
  28.       // 更新当前位置
  29.       this.currentX += deltaX;
  30.       this.currentY += deltaY;
  31.       // 更新起始点
  32.       this.startX = event.clientX;
  33.       this.startY = event.clientY;
  34.       const dialogRef = this.$refs.dialog.$el;
  35.       if (dialogRef) {
  36.         dialogRef.style.left = `${this.currentX}px`;
  37.         dialogRef.style.top = `${this.currentY}px`;
  38.         event.preventDefault();
  39.       }
  40.     },
  41.     stopDrag() {
  42.       this.dragging = false;
  43.       document.onmousemove = null;
  44.       document.onmouseup = null;
  45.     },
  46.     onDialogOpen() {
  47.       // 对话框即将打开时,重置位置数据
  48.       this.currentX = 0;
  49.       this.currentY = 0;
  50.     },
  51.     onDialogOpened() {
  52.         
  53.     },
  54.     onDialogClose() {
  55.    
  56.       // 对话框关闭时,取消所有事件监听器
  57.       document.onmousemove = null;
  58.       document.onmouseup = null;
  59.       const dialogRef = this.$refs.dialog.$el;
  60.       dialogRef.style.left= 0;
  61.       dialogRef.style.top = 0;
  62.     },
复制代码
CSS部分:
  1. <style scoped>
  2. /**以下是dialog */
  3. .draggable-dialog {
  4.   position: fixed !important; /* �保对话框是固定定位 */
  5.   top: 50%;
  6.   left: 50%;
  7.   transform: translate(-50%, -50%);
  8.   transition: all .3s ease;
  9.   overflow: auto; /* �保对话框内部可以滚动 */
  10.   /*width: 600px;  �定宽度 */
  11. /* height: 900px; /* �定高度 */
  12. }
  13. .header {
  14.   cursor: move;
  15. }
  16. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

盛世宏图

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表