question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent click propagation on drag and drop

See original GitHub issue

I was wondering if anyone had an issue with click event being fired after drag and drop.

My container is a huge list of images wrapped with links to a dedicated page.

Sometimes when the drap and drop is initiated on top of the image, the click event gets fired when releasing the mouse.

Is there an easy way to prevent this from happening?

I was thinking about adding a class no-click to the container then adding a click eventListener to all children which will check if the parent has the class no-click and then stop the propagation of the event.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:12
  • Comments:10

github_iconTop GitHub Comments

16reactions
illnrcommented, Feb 6, 2017

How about this quickfix?

_window[addEventListener](
    mouseup, cont.mu = function() {
        pushed = 0;
        // HERE
        setTimeout(function(){ el.classList.remove("dragging"); }, 100);
    }, 0
);

_window[addEventListener](
    mousemove,
    cont.mm = function(e) {
        if (pushed) {
            // HERE
            el.classList.add("dragging");
            (scroller = el.scroller||el).scrollLeft -=
                newScrollX = (- lastClientX + (lastClientX=e.clientX));
            scroller.scrollTop -=
                newScrollY = (- lastClientY + (lastClientY=e.clientY));
            if (el == _document.body) {
                (scroller = _document.documentElement).scrollLeft -= newScrollX;
                scroller.scrollTop -= newScrollY;
            }
        }
    }, 0
);

Now you can check the .dragscroll for .dragging and then prevent the click action.

$('.dragscroll a').click(function(event) {
    if ($(this).closest('.dragscroll').hasClass('dragging')) {
      event.preventDefault();
      return false;
    }
  });
0reactions
alpha-and-omegacommented, Mar 9, 2018

My workaround using click status variable (better hide it in local scope):

	var click_is_valid = true;
	$('.dragscroll').on('scroll', function() {
		click_is_valid = false;
	});

        // change to correct selector of the clickable element:
	$(".grid").on('mousedown', 'div.grid-item', function(e) {
		click_is_valid = true;
	});

	$(".grid").on('click', 'div.grid-item', function() {
		if(!click_is_valid) return;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent click event if dragged - javascript - Stack Overflow
In the first mousemove event, search all children for click event listeners, remove all of them, on mouseup restore them (possibly after a ......
Read more >
Event.stopPropagation() - Web APIs | MDN
The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.
Read more >
Stop propagation of 'drag' event for marker in Leaflet
Is there a possibility to prevent Leaflet dragstart event from triggering just moment after start dragging a marker.
Read more >
Preventing Drag.Move click propagation on child element
There are two solutions: 1. In your #parent event listener, check the event.target element. event.target is the element that was actually ...
Read more >
event.stopImmediatePropagation() | jQuery API Documentation
stopPropagation () . To simply prevent the event from bubbling to ancestor elements but allow other event handlers to execute on the same...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found