My page contains a red and a blue div:
<div id="red"></div>
<div id="blue"></div>
They're both absolutely positioned and are separated by a small amount:
#red {
background-color: red;
width: 5em;
height: 5em;
position: absolute;
left: 5em;
top: 5em;
}
#blue {
background-color: blue;
width: 5em;
height: 5em;
position: absolute;
left: 15em;
top: 5em;
}
I have some code to tell if the user clicks and drags their mouse from one div to the other:
$('#red').mousedown( function () {
$('#blue').mouseup( function () {
alert('Red to Blue')
})
})
$('#blue').mousedown( function () {
$('#red').mouseup( function () {
alert('Blue to Red')
})
})
This works perfectly the first time if the user moves the mouse directly from one div to the other with the mouse button held down.
There are 2 issues though:
- If the user releases the mouse button while outside of a
divthen clicks on the other one,mouseupwill still run. - Any time after the first, the user will have to click outside of either
divin order for the handlers to work properly.