Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  56] [ 5]  / answers: 1 / hits: 5178  / 11 Years ago, thu, december 12, 2013, 12:00:00

I am trying to drag a container using transform translate but something is causing a jumpy behavior and I can't figure out what is the cause.

UPDATE: This must work on elements when their container is not always positioned at 0,0 from the document.



http://jsfiddle.net/dML5t/2/



HTML:



<div id=container style=position:absolute;left:50px;top:50px;width:500px;height:500px;background-color:red;>
<div id=tcontainer style=position:relative;left:50px;top:50px;width:400px;height:400px;background-color:green;>
<div id=move style=position:relative;left:20px;top:20px;height:150px;width:150px;background-color:lightgray;>
</div>
</div>



Javascript:



obj = {startPositionX:0,startPositionY:0};
$('#move').on(mousedown,function(){
var move = $(this);
obj.startPositionX=event.offsetX+$('#tcontainer').offset().left;
obj.startPositionY=event.offsetY+$('#tcontainer').offset().top;
$(document).on(mousemove,function(e){
console.log(dragging, e.pageX-obj.startPositionX, e.pageY-obj.startPositionY);
move.css('transform','translate('+(e.pageX-obj.startPositionX)+'px, '+(e.pageY-obj.startPositionY)+'px)');
});
});
$(document).on(mouseup,function(){
$(this).off(mousemove);
});

More From » jquery

 Answers
2

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.



Use pageX and pageY instead! fiddle



$('.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);
});


You need save original coords of your div (move.offset()) and use mouse offset (e.pageX-startX, e.pageY-startY) to get new coords.


[#49609] Wednesday, December 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;