Mouseup not working after mousemove on img

Posted by leyou on Stack Overflow See other posts from Stack Overflow or by leyou
Published on 2012-11-05T16:42:34Z Indexed on 2012/11/05 17:00 UTC
Read the original article Hit count: 245

Filed under:
|
|

I'm trying to do a simple drag script. The idea is to save the position when the mouse is down, update the view while the mouse is moving, and stop when mouse is up. Problem, mouseup event isn't working properly.

See the code:

var target = $('a')
var pos = 0;
var dragging = false;

$(document).mousedown(function(e) { pos=e.pageX; dragging = true })
$(document).mouseup(function() { dragging = false })
$(document).mousemove(function(e) {
    if(dragging){ 
        target.css('left', e.pageX-pos);
    }
})  ?

Why mouseup works with a "a" tag: http://jsfiddle.net/leyou/c3TrG/1/

And why mouseup doesn't work with a "img" tag: http://jsfiddle.net/leyou/eNwzv/

Just try to drag them horizontally.

Same probleme on ie9, ff and chrome.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about drag