Javascript – Jquery – How to run a function onclick only if link is not dragged


I am running thickbox and jquery ui draggable both on the same page, and they are conflicting.

Jquery UI Draggable allows me to drag and drop things from one place to another with this code:

      drop: function(ev, ui) {  
             do stuff  

Thickbox starts on a click event, with essentially this code:

thickbox functions...  

But because the thickbox link is inside the draggable div, it opens the thickbox when I drag the link!! Hence, the problem.

How can I change this code so that:

  1. When I drag the link, it doesn't open thickbox.
  2. When I click the link(without dragging), it opens thickbox.


Best Solution

Check if the element is being dragged before showing thickbox. or disable thickbox event handler when dragged.


    if (is_being_dragged()) return;
    // thickbox functions...  

is_being_dragged() would have to check a flag set by the drag function. The drag function should unset the drag flag after it has completed.

You can also solve this independently of the drag function you're using by making the click function be a onmousedown, followed by onmouseup.

   this.__down = new Date().getTime(); 

   var interval = new Date().getTime() - this.__down; 
   // assume dragged if it was pressed 50 millisecs or more ago
   if (interval > 50) { return }

It is assuming a drag takes more then 50 millisecs and a click is less. You can see what works best in your case.