javascript - Why does this simple drag and drop code cause the element to flicker? -


i've got simple drag , drop jquery code vertical drag , drop:

function drag(ele){     $(document).mousemove(function(e){         ypos = ele.offset();         ypos = ypos.top;         diff = (ypos + ele.height()) - e.pagey;         ele.css('top', e.pagey - diff);     }).mouseup(function(){         $(this).unbind('mousemove');     }); } 

it works well, except starts flickering through small changes , diff variable jumps around. have no idea may causing this, perhaps else does?

http://jsfiddle.net/gtgmn/2/

the problem more prominent in jsfiddle.

it's because you're changing top property on element , @ same time you're using info move element. try example:

function drag(ele){     $(document).mousemove(function(e){         ele.css('top', e.pagey);     }).mouseup(function(){         $(this).unbind('mousemove');     }); }  $('.box').mousedown(function(){     drag($(this)); }) 

this code put element directly cursor is. might not want, try move element without depending directly on previous position.

update:

the question asked 1-1 movement click started. this fiddle that. idea calculate difference click position top of box when dragging begins, , update position using offset.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -