title: How can I drag a div and keep the cursor on the same spot as I clicked it at?
body: <p>I'm trying to drag a div when I click on it but when I do it the div blinks and moves to the left, if I remove <code>offset</code> and put <code>position</code> instead it works but the cursor goes to the left top of the div.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var selected = 0,
    x = 0,
    y = 0;
  
  $.fn.isDraggable = function() {
  
    $(this).on('mousedown', function(e) {
      selected = $(this);
      $(selected).css({
        position: 'absolute',
        left: e.pageX - $(selected).position().left,
        top: e.pageY - $(selected).position().top
      });
    });
    $(document).on('mouseup', function() {
      if (selected !== 0) {
        selected = 0;
      }
    });
    $(document).bind('mousemove', function(e) {
      $(selected).css({
        position: 'absolute',
        left: e.pageX - $(selected).offset().left,
        top: e.pageY - $(selected).offset().top
      });
    });
  
    return true;
  };
  
  
  $('#card').isDraggable();</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#card {
    position: fixed;
    width: 100px;
    height: 150px;
    top: calc(50% - 75px);
    left: calc(50% - 50px);
    border: 1px solid #D3D3D3;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
    &lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
    &lt;title&gt;freeMarketRocks&lt;/title&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div&gt;
  
      &lt;div id="card"&gt;
      &lt;/div&gt;
  
    &lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>