How can I create an event in JQuery triggered when the mouse is down and moving? And only triggered once each mousedown + mousemove?
A:
May be:
$('input').mousedown(function{
$('input').mousemove({
etc
});
});
but for some reason i think you already try that..
Noam Smadja
2009-10-15 13:57:15
ok, so $('input').bind('mousedown mousemove', fn); will only fire when the mouse is pressed down AND moving? I thought this would fire if the mouse is pressed down OR moving.
Persmon
2009-10-15 14:07:43
+1
A:
Updated:
So, it looks like if your mouse is no longer over the element on which onmouseup is bound, it won't see the mouse up event. Makes sense, when you stop and think about it, but when the mousedown event happens over the element, we expect, as UI users, for it to know when it was released (even if it isn't over the element).
So, to get around this, we actually detect the mouseup on the document level.
var clicking = false;
$('.selector').mousedown(function(){
clicking = true;
$('.clickstatus').text('mousedown');
});
$(document).mouseup(function(){
clicking = false;
$('.clickstatus').text('mouseup');
$('.movestatus').text('click released, no more move event');
})
$('.selector').mousemove(function(){
if(clicking == false) return;
// Mouse click + moving logic here
$('.movestatus').text('mouse moving');
});
I tried this out on jsbin, and it seems to work. Check it out here: http://jsbin.com/icuso. To edit it (see the JS and HTML), just tag "edit" on the end of the URL. http://jsbin.com/icuso/edit.
Matt
2009-10-15 14:23:32
the problem is that the mouseup doesn't seem to be triggered after I have dragged the mouse, then the clicking isn't reset
Persmon
2009-10-15 14:41:37
How interesting - I didn't realize that. I just tested it on FF3.5 and it is as you say. Which browser are you using?
Matt
2009-10-15 15:00:38
Yeah, its firefox.And it gets worse. I'm working with drag and drop from an iframe and out to the parent. So I guess I'll need a mouseup trigger in the parent aswell, which will trigger mouseup in the iframe. I'll try this
Persmon
2009-10-15 15:12:17