473,419 Members | 4,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Are simultaneous mouseover and mousemove events possible?

JB
I am struggling to figure out a way to allow one element
to be dragged, but still capture 'mouseover' events on
other elements.

I've created a simple example to demonstrate what I mean:
http://gerromorpha.cs.uoregon.edu/moveAndOver.html
(The above link is only meant for Windows/Firefox.)

It's not much code, but it's probably too much to paste here.
The gist of it is, when a user clicks on element #1, I attach
the mousemove event to element #1. That event remains
attached until the user un-clicks (mouseup). During the time
the user is dragging element #1, however, element #2 doesn't
register the mouseover events it should.

Any ideas on how to get around this? If I should be approaching
this in a completely different way I would appreciate some
alternatives. Any response, for that matter, will be greatly
appreciated.

Mar 17 '06 #1
5 2373
You know, I was working on this very thing a couple weeks ago to
implement classic drag and drop. Luckily for you I have explored this
issue extensively and can save you hours of researching by telling you
what I found out.

The short of it all, you can't. Both IE and FF just don't allow it
right now. What you do need is a collusion function which will tell you
when one item (the item you are dragging) has collided (is over)
another element. When the two elements collide, you can call a function
which will give you the same effect as mouseover on the second element.

I got this off a guy here on the forums, and even though it is a bit
unreadable, it works like a charm and allows me to do the drag and drop
I need.

hitTest = function(o, l){
function getOffset(o){
for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth,
b: o.offsetHeight};
o = o.offsetParent; r.l += o.offsetLeft, r.t +=
o.offsetTop);
return r.r += r.l, r.b += r.t, r;
}
for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j
? l = [l] : l).length; i;
b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r :
b.l <= a.r))
&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) &&
(r[r.length] = l[i]));
return j ? !!r.length : r;
};

Use: hitTest(obj1,obj2)
Description: Returns true and false if the reference to obj1 and obj2
overlap or "collided".

You would put this in your mousemove event within an if statement. If
it is true, call the function you want to call as if it was in a
mouseover of the second element.

Good luck! :)

Mar 18 '06 #2
VK

JB wrote:
I am struggling to figure out a way to allow one element
to be dragged, but still capture 'mouseover' events on
other elements.

I've created a simple example to demonstrate what I mean:
http://gerromorpha.cs.uoregon.edu/moveAndOver.html
(The above link is only meant for Windows/Firefox.)


In addition to the previous answer:

It should be clear that the problem is *not* that mouse events are not
reaching div2. Events are going Russian hills (down to bottom and up to
high) in Firefox and Bubble way (from the bottom to high) in IE, and
nothing is changed while dragging. As your current drag script is
vulnerable to the "drag off the body" trick, you can see it yourself
(and would be nice to fix it in the release of course). Start dragging
div1 to the menu bar until it stops. Release button an return mouse to
the screen. div1 is still in dragging mode but doesn't move (you may
need to repeat this by changing the return path for the mouse to get
this). Now if you move your mouse over div2, the mouseover event will
be properly captured.

So the problem is not in event capturing, but in the fact that while
dragging the mouse is not over div2 - it is over div1. div1 is over
div2 - that's true, but there is no handler like onDiv1Over to capture
:-)

So you need to use coords rectangle check as suggested for FF and
others. For IE it is enough to use the native
document.elementFromPoint(x,y) method.

Mar 18 '06 #3
JB
Wow. That is some serious abuse of for-loops.
It does seem to mostly work. I have translated
it into more readable code and I will post it here
for future reference.

I am not sure I understand exactly what's going on,
but I think if obj2 (in my translated code) is a single
object, it's converted into a single-element array.
Otherwise, if the user passed an array of objects,
hitTest() detect collisions with all the elements in
that array. I have tried sending the function an
array of objects but I couldn't get it to work.

hitTest = function(obj1, obj2){
function getOffset(obj){
var objBounds = {
objLeft: obj.offsetLeft,
objTop: obj.offsetTop,
objRight: obj.offsetWidth,
objBottom: obj.offsetHeight
};

for( ; obj = obj.offsetParent; )
{
objBounds.objLeft += obj.offsetLeft;
objBounds.objTop += obj.offsetTop;
}

objBounds.objRight += objBounds.objLeft
objBounds.objBottom += objBounds.objTop

return objBounds;
}

var obj2Bounds
var collisionArray = [];
var obj1Bounds = getOffset(obj1);

// My guess:
// If obj2 is a single object, convert it to a single
// element array. Now that obj2 is definitely an
// array, set i to the length of that array
var j = isNaN(obj2.length);
var i = (j ? obj2 = [obj2] : obj2).length

for(; i; )
{
obj2Bounds = getOffset(obj2[--i])

if ( (obj1Bounds.objLeft == obj2Bounds.objLeft ||
(obj1Bounds.objLeft > obj2Bounds.objLeft ?
obj1Bounds.objLeft <= obj2Bounds.objRight :
obj2Bounds.objLeft <= obj1Bounds.objRight))
&& (obj1Bounds.objTop == obj2Bounds.objTop ||
(obj1Bounds.objTop > obj2Bounds.objTop ?
obj1Bounds.objTop <= obj2Bounds.objBottom :
obj2Bounds.objTop <= obj1Bounds.objBottom)))
{
collisionArray[collisionArray.length] = obj2[i];
}
}

// My guess:
// if obj2 is a single object and it collides with obj1,
// collisionArray.length == 1 so !!collisionArray.length == true
// if obj2 is an array of objects, return an array of
// the subset of obj2 objects with which obj1 collidess
return j ? !!collisionArray.length : collisionArray;
};
Thanks for your post, you saved me a lot of pain.

Martyr2 wrote:
You know, I was working on this very thing a couple weeks ago to
implement classic drag and drop. Luckily for you I have explored this
issue extensively and can save you hours of researching by telling you
what I found out.

The short of it all, you can't. Both IE and FF just don't allow it
right now. What you do need is a collusion function which will tell you
when one item (the item you are dragging) has collided (is over)
another element. When the two elements collide, you can call a function
which will give you the same effect as mouseover on the second element.

I got this off a guy here on the forums, and even though it is a bit
unreadable, it works like a charm and allows me to do the drag and drop
I need.

hitTest = function(o, l){
function getOffset(o){
for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth,
b: o.offsetHeight};
o = o.offsetParent; r.l += o.offsetLeft, r.t +=
o.offsetTop);
return r.r += r.l, r.b += r.t, r;
}
for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j
? l = [l] : l).length; i;
b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r :
b.l <= a.r))
&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) &&
(r[r.length] = l[i]));
return j ? !!r.length : r;
};

Use: hitTest(obj1,obj2)
Description: Returns true and false if the reference to obj1 and obj2
overlap or "collided".

You would put this in your mousemove event within an if statement. If
it is true, call the function you want to call as if it was in a
mouseover of the second element.

Good luck! :)


Mar 19 '06 #4
JB
> So the problem is not in event capturing, but in the fact that while
dragging the mouse is not over div2 - it is over div1. div1 is over
div2 - that's true, but there is no handler like onDiv1Over to capture


That makes perfect sense. I feel stupid for not thinking of that.

Thanks for your reply.

Mar 19 '06 #5
JB said on 20/03/2006 7:24 AM AEST:
Wow. That is some serious abuse of for-loops.
It does seem to mostly work. I have translated
it into more readable code and I will post it here
for future reference.


I've done a similar thing by creating a object that contains 'drop
zones'. As you drag one element, you see if it's over a drop zone. By
storing the co-ords of each drop-zone (and updating them if/when they
move) the algorithm for seeing if the dragged element is 'over' a drop
zone is much faster than finding the co-ords from scratch every time.

If you have a lot of drop zones, give them a key based on their
co-ordinates. Then keep the keys in an array and use either a btree or
quadtree search to find 'hits'. That will allow you to keep track of
hundreds of drop zones without too much processing overhead.
[...]
--
Rob
Mar 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Crucifix | last post by:
Hello, I'm writing a small C# app, and part of what I'm trying to do involves the dragging of PictureBox controls on a form. Unfortunately, MouseMove seems to be behaving very oddly, causing...
7
by: rkbnair | last post by:
Is it possible to change the background color of the DataGrid on pointing the mouse over a certain row?
3
by: TyBreaker | last post by:
I'm writing a screen saver using Visual Studio 2005 (Basic) and I have a Form which contains a PictureBox. I have two events, Click and MouseMove that I'd like to cause the program to end (see...
1
sgeklor
by: sgeklor | last post by:
Dear All, My question refers to how best to organise my mouse events to correctly and efficiently give the effect I want. On my form I have a panel and that panel contains a number of controls....
18
by: Shocky | last post by:
Hi there, I am trying to detect whether my IE6 users have pressed both right and left mouse buttons simultaneously in my Javascript code, by using: if(event.button==3) {alert("Both right and...
2
by: markszlazak | last post by:
I'm a relatively slow response of table cells changing their background color with mouseover/our in IE6 (Win 2K) but the response is fine (fast) in Firefox. Why? The code is below. Sorry about the...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
2
by: Lazarus | last post by:
Hi, I have a canvas that reacts to mousedown and mousemove. Once I click the canvas I want to track the cursor wherever it goes, including outside the browser. Setting window.onmousemove only...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.