Prevent click event after drag in jQuery

With an ES6 class (No jQuery)

To achieve this in javascript without the help of jQuery you can add and remove an event handler.

First create functions that will be added and removed form event listeners

flagged () {
    this.isScrolled = true;
}

and this to stop all events on an event

preventClick (event) {
    event.preventDefault();
    event.stopImmediatePropagation();
}

Then add the flag when the mousedown and mousemove events are triggered one after the other.

element.addEventListener('mousedown', () => {
    element.addEventListener('mousemove', flagged);
});

Remember to remove this on a mouse up so we don't get a huge stack of events repeated on this element.

element.addEventListener('mouseup', () => {
    element.removeEventListener('mousemove', flagged);
});

Finally inside the mouseup event on our element we can use the flag logic to add and remove the click.

element.addEventListener('mouseup', (e) => {
    if (this.isScrolled) {
        e.target.addEventListener('click', preventClick);
    } else {
        e.target.removeEventListener('click', preventClick);
    }
    this.isScrolled = false;
    element.removeEventListener('mousemove', flagged);
});

In the above example above I am targeting the real target that is clicked, so if this were a slider I would be targeting the image and not the main gallery element. to target the main element just change the add/remove event listeners like this.

element.addEventListener('mouseup', (e) => {
    if (this.isScrolled) {
        element.addEventListener('click', preventClick);
    } else {
        element.removeEventListener('click', preventClick);
    }
    this.isScrolled = false;
    element.removeEventListener('mousemove', flagged);
});

Conclusion

By setting anonymous functions to const we don't have to bind them. Also this way they kind of have a "handle" allowing s to remove the specific function from the event instead of the entire set of functions on the event.


FIRST attach the draggable event, THEN the click event:

$(function(){
    $('div').draggable();
    $('div').click(function(){
        $(this).toggleClass('orange');
    });
});

Try it here: http://jsfiddle.net/aG72R/55/