title: jQuery - how to animate draggable div towards moving cursor during drag event
body: <p>I am using jquery UI draggable and want to have a draggable div animate during a drag event. Imagine that when stationary, the square div in the example is attached to a magnet. Wen you click on it and start dragging, it will not drag until a certain distance threshold is reached (distance = distance between center of div and current mouse position). After that threshold is reached, the div will animate toward the mouse and should then proceed to a normal drag event.</p>
  
  <p>My problem is that when the mouse is dragged past the distance threshold at a fast enough speed, the div will start flashing because it is switching between showing the animated div and the one that is being dragged. Another problem that occurs is that if you drag the mouse fast enough and then stop, the div animates to the last recorded mouse position, but by the time the calculation is made, the mouse might already be at a different position, so the mouse is at one spot and the div is at another.  Any ideas on how to make the div animate towards the mouse and then continue the drag event in one smooth transition? (also, i want the animation do be long enough so that you can see the div moving. if duration is set to 1, it works fine but I want it to be more visually appealing and smooth) Here is a demo: <a href="http://jsfiddle.net/b84wn2nf/" rel="nofollow">http://jsfiddle.net/b84wn2nf/</a></p>
  
  <p>Here is some of the code found in the demo:</p>
  
  <pre><code>$(".dragSquare").draggable({
  drag: function (event, ui) {
  
      if($(this).hasClass("stuck")){
      var mx = event.pageX;
      var my = event.pageY;
      ui.position.left = $(this).position().left;
      ui.position.top = $(this).position().top;
  
      var d = Math.floor(Math.sqrt(Math.pow(mx - ($(this).offset().left + ($(this).width() / 2)), 2) + Math.pow(my - ($(this).offset().top + ($(this).height() / 2)), 2)));
      console.log(d)
      if (d &gt; 200) {
          var animateToX = event.pageX - $(this).width() / 2;
          var animateToY = event.pageY - $(this).height() / 2;
  
          $(this).stop(true, true).animate({
              left: animateToX + 'px',
              top: animateToY + 'px'
          }, {
              /*easing: 'easeOutBounce,'*/
              duration: 500,
              start: function () {},
              complete: function () {
                  $(this).removeClass("stuck");
              }
          });
      }
      }
  } });
  </code></pre>