当前位置: 首页 > news >正文

vue3实现拖拽移动位置,拖拽过程中鼠标松开后元素还吸附在鼠标上并随着鼠标移动

发现问题

拖拽元素移动的时候,偶尔会出现拖拽过程中鼠标松开后元素还吸附在鼠标上并随着鼠标移动,要再按一下元素才会被放置下来。但是有时就正常。

问题分析

出现该问题的原因是:这个过程会触发H5原生的拖拽事件,并且不会监听到onmouseup,从而导致鼠标松开也能够拖拽。

应该阻止h5的拖拽事件:

document.ondragstart = function(ev) {ev.preventDefault();
};
document.ondragend = function(ev) {ev.preventDefault();
};

在相应的位置添加这2个函数即可。

let move = false;
function mousedown(event, c, type, cells) {tmp = c;if (type === vqEnums.funcType.Condition) {if ((c.Index === props.conditions.length - 1 &&c.Index - 1 >= 0 &&props.conditions[c.Index - 1].FuncType === vqEnums.funcType.Logic) ||(c.Index - 1 >= 0 &&props.conditions[c.Index - 1].FuncType === vqEnums.funcType.Logic &&c.Index + 1 <= props.conditions.length - 1 &&props.conditions[c.Index + 1].AggregateFunc)) {tmpLogicType = props.conditions[c.Index - 1].LogicType;}}if (event.shiftKey && event.which === 1) {cellClick(event, c, cells);} else if (event.which === 1 && !event.ctrlKey && !event.shiftKey) {if (c.FuncType === vqEnums.funcType.Logic ||c.FuncType === vqEnums.funcType.Bracket)return;let _target = $(event.target).hasClass('c-icon')? $(event.target)[0].parentNode: event.target;let cell = c;let startx = event.x - _target.getBoundingClientRect().x;let starty = event.y - _target.getBoundingClientRect().y;let scrolly = $(_target).parent().scrollTop();let cx = _target.getBoundingClientRect().x;let cy = _target.getBoundingClientRect().y;let ww = document.documentElement.clientWidth;let wh = window.innerHeight;let tWidth = _target.getBoundingClientRect().width;let w = tWidth + 8;if ($(_target).hasClass('cell-margin')) {w += 20;}const length = $(_target).next().length;if (length > 0) {if (_target.getBoundingClientRect().y ==$(_target).next()[0].getBoundingClientRect().y) {$(_target).next().css({ marginLeft: w + 'px' });}$(_target).css({position: 'fixed',top: _target.getBoundingClientRect().y - 2,left: _target.getBoundingClientRect().x,'z-index': 999});} else {$(_target).css({// position: 'fixed',top: _target.getBoundingClientRect().y - 2,left: _target.getBoundingClientRect().x,'z-index': 999});}$(_target).siblings().css({ transition: 'margin 0.3s' });$(_target).removeClass('cell-margin');document.onmousemove = function (ev) {if (ev.clientY < 0 ||ev.clientX < 0 ||ev.clientY > wh ||ev.clientX > ww) {mouseup(event, c, type);return false;}if (Math.abs(ev.screenX - event.screenX) >= 5 ||Math.abs(ev.screenY - event.screenY) >= 5 ||move) {if (Math.abs(event.x - ev.x) < 2 ||(Math.abs(event.y - ev.y) < 2 && !move)) {move = false;return false;}if (length == 0) {$(_target).css({position: 'fixed'});}move = true;let endx = ev.x - cx - startx;let endy = ev.y - cy - starty; // - scrollylet siblings = Array.from($(_target).siblings());let cells =type === vqEnums.funcType.Out? props.outs: type === vqEnums.funcType.Condition? props.conditions: props.sorts;setTransform(ev, _target, siblings, cell, cells, tWidth, scrolly);_target.style.transform = 'translate(' + endx + 'px,' + endy + 'px)';}};document.ondragstart = function (ev) {ev.preventDefault();};document.ondragend = function (ev) {ev.preventDefault();};}
}

http://www.lryc.cn/news/434024.html

相关文章:

  • 没有屋檐的房子-011
  • Puppeteer-Cluster:并行处理网页操作的新利器
  • 使用Protocol Buffers传输数据
  • chmod修改文件权限
  • 二叉树--python
  • matlab数据批量保存为excel,文件名,行和列的名称设置
  • Pygame中Sprite类实现多帧动画3-2
  • C#发送正文带图片带附件的邮件
  • 【C#跨平台开发详解】C#跨平台开发技术之.NET Core基础学习及快速入门
  • 请解释Java中的死锁产生的原因和解决方法。什么是Java中的并发工具类?请列举几个并解释其用途。
  • 三分钟带你看懂,低代码开发赋能办公方式转变
  • 视频剪辑软件哪个好用?11款软件轻松上手,让创意视频流畅呈现!
  • pytest二次开发:生成用例参数
  • 想抹黑华为的 请换一种方式
  • 学习学习学习
  • requestAnimationFrame原理和使用
  • 线程的状态(java)
  • Linux IO模型:IO多路复用
  • [数据集][目标检测]电梯内广告牌电动车检测数据集VOC+YOLO格式2787张4类别
  • MATLAB下载详细教程及下载链接
  • 利用发电量和气象数据分析来判断光伏仿真系统的准确性
  • Model-based RL动态规划(基于价值、基于策略,泛化迭代)
  • 外接串口板,通过串口打开adb模式
  • ssm微信小程序校园失物招领论文源码调试讲解
  • iOS 15推出后利用邮件打开率的7种方法
  • 以太网--TCP/IP协议(一)
  • LeetCode刷题:找到第K大的元素
  • HTML页面配置高德地图,获取位置
  • HTTrack
  • 干货分享|分享一款微软出品的工作效率神器 PowerToys