Javascript Hover Function For Submenu
Solution 1:
It doesn't answer your specific question but the same behavior can be easily achieved with css. This way you don't depend on javascript being turned on for standard menu access.
ul.menuliul {
display: none;
}
ul.menuli:hoverul {
display: block;
position: relative;
}
<ulclass="menu"><li><ahref="#">Home</a></li><li><ahref="#">Galleries</a><ul><li>Gallery #1</li><li>Gallery #2</li></ul></li><li><ahref="#">Albums</a><ul><li>Album #1</li><li>Album #2</li></ul></li></ul>
View on jsFiddle
Solution 2:
You are using hide and show wrong. http://api.jquery.com/show/http://api.jquery.com/hide/
$('#top-menu').hover(
function () {
$('#submenu').show();
},
function () {
$('#submenu').hide();
}
);
Solution 3:
id
must be unique. If you have multiple elements with the same id
, jquery will not retrieve all the elements when you do $('#top-menu'), it'll only find the first element that matches the selector.
Solution 4:
We're going to need to change the HTML a bit. IDs are used only once on a page. Classes are similar, but can be applied to any number of elements. We also want to nest our sub-menu's under the top-menu. That way the association is more clear.
<liclass="top-menu"><ahref="#">About Us</a><ulclass="sub-menu non-active"><li>Ashley + David</li><li>Our Style</li><li>The Experience</li></ul></li>
We want to specify the nested sub-menu to show or hide. $(this)
refers to the top-menu that was hovered over.
$('.top-menu').hover(
function () {
$(this).find('.sub-menu').show("slow");
},
function () {
$(this).find('.sub-menu').hide("slow");
}
);
demo
Solution 5:
I updated your work. Is this what are trying to establish?
$('#top-menu').mouseover(function(){
$('#submenu').addClass('active');
});
$('#top-menu').mouseout(function(){
$('#submenu').removeClass('active');
});
Post a Comment for "Javascript Hover Function For Submenu"