hi all im building a menu and im trying to put an hover effect on each menu item
but… when i hover everything is working great and i get the html i want and the menu item has a backgruond image.
but the unhover effect dosnt fire most of the times. i found out that if i move my mouse horziantly across the ul menu it works fine. but if my mouse does a vertical move across the li item it dosnt fire.
my code is this:
$("ul.menu li").hover(ChangeToHoverMenuItem, ChangeBackMenuItem);
function ChangeToHoverMenuItem()
{
var currLi = $(this);
lastLi = currLi;
var currMenuItemText = currLi.find("a").text();
currLi.html("");
currLi.append("<div style='float:right;'><div class='right_item_hov'></div>" +
"<a class='item_menu_hov'>" +
currMenuItemText +
"</a>" +
"<div class='left_item_hov'></div></div>");
}
function ChangeBackMenuItem ()
{
var currLi = $(this);
var currMenuItemText = currLi.find("a").text();
currLi.html("");
currLi.append("<a>" + currMenuItemText + "</a>");
}
<div class="menu_middle">
<ul class="menu">
<li>
<a>
main
</a>
</li>
<li>
<a>
gallery
</a>
</li>
<li>
<a>
event
</a>
</li>
<li>
<a>
about
</a>
</li>
<li>
<a>
contact
</a>
</li>
</ul>
</div>
thank you
The strange thing is, that there are two
mouseenter()events on sibling elements without amouseleave()event in between.One idea is to register the
mouseenter()andmouseleave()event instead of usinghover().hover()should do the same, but how knows. If this works, the problem lies in the implementation ofhover()but I don’t think so.Another idea is to ensure that the
mouseleave()code was executed. In your “doHover” handler you can check if there is anhoveredItem. If so, you can call theChangeBackMenuItemfunction with the still hovered item asthis. To ensure that the “unhover” handler is called when you leave your ul, you need amouseleaveevent on this, that does the same. This solution relies on a workingmouseleaveon the ul element.