and I have two JS functions.
The unexpected behavior-problem is that wherever the pointer moves between the the <li> elements (next or previous), the whole hide_menu() and show_menu() is executed as if the pointer got out of the div space and re-entered.
Any idea why this happens?
Representation:
+—————————————+
|…………………………………………| That's my div. No matter what's displayed in the
+—————————————+ DIV, either the <p> or the <ul>, as long as the mouse doesn't leave the block is shouldn't run the js again and again.
So, when my DIV is like
+—————————————+
|LI~LI~LI~LI~LI~LI~LI~LI|
+—————————————+
when the mouse goes from one LI to another, the JS is run again and again. Why?
There is no hint of bubbling or capturing, as the is only one handler, the one of the DIV.
The problem might be that when you open the menu. If the menu opens under the mouse then you are effectively leaving the <li> since the mouse just entered the menu's <div>.
OK but since the menu thing is contained in another div, no matter if I leave the LI space, it shouldn't execute, since the handlers are for leaving the container div space.
Thing is, even if the menu is contained within the <div> with the event, moving the mouse over that <li>, or <ul> or <p> triggers the mouse out.
Remember each HTML element is like a layer (w/o involving CSS position and z-index).
The <div> is at the lowest level while the <p> and <ul> are above it. Even if they are within <div> they are a different element, therefore triggering the mouse out event of the <div>.
Yes, like vwphillips shows, you have to use event propagation. That is, when the mouse out is detected check to see whether is that element belongs or not to the one who called it.
Check the script made by vwphillips:
Code:
function hide_menu(obj)
{
var e = window.event || arguments.callee.caller.arguments[0];
if (e)
{
var eobj = e.relatedTarget || e.toElement;
if (eobj)
{
while (eobj.parentNode)
{
if (eobj==obj)
{
return false;
}
eobj = eobj.parentNode;
}
}
}
document.Show.Show0.value = vic++;
}
Works as follows:
When the mouseout event is called grab the window.event and lets see who called it (obj).
Now, let's see which element was focused that triggered the mouseout event (eObj).
While eObj has a parentNode, cycle through them. If at any point you find that eObj is contained withing obj (eObj == obj), then stop the script (return false), other wise, close the menu.
Bookmarks