Javascript JS-如何在右键单击可拖动模式后取消固定鼠标?
我正在使用以下代码拖动模态:Javascript JS-如何在右键单击可拖动模式后取消固定鼠标?,javascript,modal-dialog,Javascript,Modal Dialog,我正在使用以下代码拖动模态: function makeDivDraggable(modal) { let x = 0; let y = 0; // Query the element const ele = modal; ele.style.cursor = "move"; // Handle the mousedown event // that's triggered when user drags the element c
function makeDivDraggable(modal) {
let x = 0;
let y = 0;
// Query the element
const ele = modal;
ele.style.cursor = "move";
// Handle the mousedown event
// that's triggered when user drags the element
const mouseDownHandler = function(e) {
// Get the current mouse position
x = e.clientX;
y = e.clientY;
// Attach the listeners to `document`
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
};
const mouseMoveHandler = function(e) {
// How far the mouse has been moved
const dx = e.clientX - x;
const dy = e.clientY - y;
// Set the position of element
ele.style.top = `${ele.offsetTop + dy}px`;
ele.style.left = `${ele.offsetLeft + dx}px`;
// Reassign the position of mouse
x = e.clientX;
y = e.clientY;
};
const mouseUpHandler = function() {
// Remove the handlers of `mousemove` and `mouseup`
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
};
ele.addEventListener('mousedown', mouseDownHandler);
};
}
除了一件小事之外,一切都很好
如果在模式中有一些链接,由于\u target=“blank”
的作用,这些链接可以很好地打开到新选项卡。但是,如果我右键单击链接,并选择打开到新选项卡,则选项卡将打开,但div仍保持“单击”,而不是未单击,这意味着无论光标移动到哪里,即使没有单击鼠标按钮,模式也会跟随光标
我正在考虑一个解决方案:
-忽略右键单击并使其仅通过左键单击移动。
-或取消鼠标锁定-右键单击完成后立即删除事件侦听器
你能提出一个解决方案吗?如何编辑代码使其工作