Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OffsetX and OffsetY may give you cursor pos relative to an element which is hovered now. You saved initial coordinates in mousedown. When mousemove was triggered your this coordinates changed a little, so when you subtract one from initials you got zeros (or 1px of difference) and your div went to top left corner. After it happaned your cursor hovered body element and in mousemove you get coordinates related to body. So when you subtract your zeros from the new coordinates you get real coordinates and your div go to the right place. Then you will get coordinates related to dragging div and will get zeros again, then real coords and so on. </p> <p>Use pageX and pageY instead! <a href="http://jsfiddle.net/8tHXe/7/" rel="nofollow">fiddle</a></p> <pre><code>$('.move').on("mousedown",function(me){ var move = $(this); var lastOffset = move.data('lastTransform'); var lastOffsetX = lastOffset ? lastOffset.dx : 0, lastOffsetY = lastOffset ? lastOffset.dy : 0; var startX = me.pageX - lastOffsetX, startY = me.pageY - lastOffsetY; $(document).on("mousemove",function(e){ var newDx = e.pageX - startX, newDy = e.pageY - startY; console.log("dragging", e.pageX-startX, e.pageY-startY); move.css('transform','translate(' + newDx + 'px, ' + newDy + 'px)'); // we need to save last made offset move.data('lastTransform', {dx: newDx, dy: newDy }); }); }); $(document).on("mouseup",function(){ $(this).off("mousemove"); }); </code></pre> <p>You need save original coords of your div (<code>move.offset()</code>) and use mouse offset (<code>e.pageX-startX</code>, <code>e.pageY-startY</code>) to get new coords. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload