views:

142

answers:

1

I'm working on a web app where I support dragging on the page, by simply watching for mousedown/mousemove/mouseup events. That part works great.

The problem is that I'm only handling dragging inside one particular element, by design, and if a user (accidentally or not) drags outside, it gets "stuck" in drag mode. That is, mouse-down, then mouse-leaves-window, then mouse-up, then mouse-returns to window looks like it's still dragging, to my app.

I haven't figured out any way to solve this -- even something simple like "when the mouse re-enters the window, is the mouse button down?" would work.

Does such functionality exist, and I'm just missing it? Or is there some clever workaround I can employ here?

Legacy support has no importance to me -- if it's an HTML5 solution that only works in FF3.5/Chr4/Sf4, I'm happy with that.

+1  A: 

What if you had the onmouseout event of the element fire the mouseup event?

If you're just using inline handlers, something along the lines of:

<div id='dragElement' onmouseup='alert("stop dragging!")' onblur='this.onmouseup();'></div>

added to whatever event handling code you're already using. This would 'release' the drag whenever the element loses focus. Not the cleanest code, but you get the idea.

Ryan Mitchell
It's not a bad idea, but I've tried something like this, and I'm not sure it's better than getting stuck in drag-mode. When you drag back into the area, I can't figure out how to detect "is mouse button down", so it has to assume the mouse button is up. Since I'm basically allowing the user to draw in an area (guess I should have mentioned that!), if/when you go out of the area by a little, you lose your drag. I guess half the time the user will want to keep dragging, and half the time they'll want to keep not-dragging, and this is alway-not (rather than alway-drag).
Ken
What if you put the onmouseup handler on a higher-level div? That way you could only begin a drag within the element you're drawing in, but you could stop a drag from anywhere. If you started a drag, left the drawing area, and released the mouse, dragging would stop, and you'd need to restart the drag from within the drawing area. Is that the behavior you're looking for?
Ryan Mitchell