2

I work with this, and realized that links always get fired if you drag them. I would like to avoid that, and coded this:

$("div").drag(function() {
  alert("Link should not be clickable while dragging.");
  $("div a").css("pointer-events", "none");
});
div {
  padding: 10px;
  background-color: yellow;
  display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div><a href="https://www.wikipedia.org/">Wikipedia</a></div>

Unfortunately, the .drag(function() { does not exist. Is there another way to code it like that?

And yes, I have already read this article. I have tried all what is written there, but was not able to fix it.

Would be very thankful for help! <3

Anna_B
  • 820
  • 1
  • 4
  • 23
  • hi, interesting, perhaps customize `ui-draggable-dragging` class with https://api.jqueryui.com/draggable/ – IronMan Mar 19 '21 at 00:21
  • @IronMan – I just tried it. It doesn't work. Destroys the layout completely, Box2D is not compatible with the jQuery UI draggable function. – Anna_B Mar 19 '21 at 00:30
  • with zimjs you can capture the `mousedown` event `myObject.on('mousedown', e => { ... });` – IronMan Mar 19 '21 at 00:44
  • @IronMan It's also possible with jQuery. But if I do that, then the links are not clickable at all anymore. – Anna_B Mar 19 '21 at 00:54

1 Answers1

0

this could be a solution, the idea is to capture the event and prevent it if there was a pointer movement:

// solution
(() => {
  let lock = 0;
  const prevent = e => {
    if (lock !== 2)
        return;
    e.preventDefault();
    e.stopPropagation();
  };
  const unlock = e => {
    prevent(e);
    setTimeout(() => lock = 0);
  };
  mydiv.addEventListener('click', prevent, true);
  mydiv.addEventListener('pointerdown', e => lock = 1, true);
  mydiv.addEventListener('pointermove', e => lock && (lock = 2), true);
  mydiv.addEventListener('pointerup', unlock, true);
  mydiv.addEventListener('pointercancel', unlock, true);
})();
#mydiv {
  position: absolute;
  z-index: 9;
  background-color: #f1f1f1;
  text-align: center;
  border: 1px solid #d3d3d3;
}

#mydivheader {
  padding: 10px;
  cursor: move;
  z-index: 10;
  background-color: #2196F3;
  color: #fff;
}
some random drag example
<div id="mydiv">
  <a href="https://www.wikipedia.org/">Wikipedia</a>
  <p>Move</p>
  <p>this</p>
  <p>DIV</p>
</div>

<script>
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    elmnt.onmousedown = dragMouseDown;
  }
  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;
  }
  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }
  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
</script>
n--
  • 3,563
  • 4
  • 9
  • Thanks for your answer! A further question: Is it possible to take your »solution« for only one div, and not for the whole page? – Anna_B Mar 19 '21 at 04:28
  • if that `div` is the parent of anchor element then yes, just add all those listeners to `div` not to `window`, see updated answer – n-- Mar 19 '21 at 07:08
  • Thank you for your comment! I created this question for that: https://stackoverflow.com/q/66708104/12501851 – Would be very thankful if you could help me again! – Anna_B Mar 19 '21 at 12:23
  • just saw your comment, I see the issue is already solved, so weird, it was a simple hint which I shared in my last update but it got more votes than a complete solution which I had to code and test :) – n-- Mar 19 '21 at 13:09