Reputation: 2160
I've decided to have a trickier approach to coding a menu. Seems I like to torture myself.
Anyhow, I created a div menu:
<div class="menu-bar">
<div class="menu-item"><span class="menu-text">HOME</span></div>
<div class="menu-item"><span class="menu-text">ABOUT US</span>
<div class="sub-menu-items">
<div class="sub-menu-item">History</div>
<div class="sub-menu-item">Mission, vision and values</div>
<div class="sub-menu-item">B-BBEE</div>
<div class="sub-menu-item">Team</div>
<div class="sub-menu-item">Professional Affiliations</div>
</div>
</div>
<div class="menu-item"><span class="menu-text">SECTORS</span></div>
<div class="menu-item"><span class="menu-text">SERVICES</span></div>
<div class="menu-item"><span class="menu-text">CSR</span></div>
<div class="menu-item"><span class="menu-text">PROJECTS</span></div>
<div class="menu-item"><span class="menu-text">SUSTAINABILITY</span></div>
<div class="menu-item"><span class="menu-text">CONTACT US</span></div>
</div>
My issue comes in with the sub menu .sub-menu-items
. I have no idea how to make it stay open once the mouse moves out of the About us block and into the sub menu.
Here is the current Javascript code I have:
$('.sub-menu-items').hide();
$('.menu-text').hover(function(e) {
// Show highlight
$(this).toggleClass('menu-text-hover');
$(this).parent().toggleClass('menu-item-hover');
if($(this).html().indexOf('ABOUT',0) !== -1)
$('.sub-menu-items').show('fast');
},function(e) {
// Hide highlight
$(this).toggleClass('menu-text-hover');
$(this).parent().toggleClass('menu-item-hover');
if($(this).html().indexOf('ABOUT',0) !== -1)
$('.sub-menu-items').hide('fast');
});
And here is my CSS:
.menu-bar { position:absolute; top:159px; height:54px; width:1024px; background-color:#fafafa; z-index:2; }
.menu-item { display:table-cell; vertical-align:middle; position:relative; left:79px; height:54px; text-align:center; width:105px; border-right:dotted thin #000; border-bottom:dotted thin #000; background-color:#fafafa; }
.menu-item-hover { border-bottom:none; }
.menu-text { display:table-cell; vertical-align:middle; height:52px; width:101px; position:relative; left:2px; top:1px; text-align:center; border-top-right-radius:0px; margin-left:2px; margin-top:2px; margin-right:2px; }
.menu-text-hover { border-top-right-radius:20px; background-color:#445921; color:#fff; cursor:pointer; }
.sub-menu-items { position:absolute; display:inline-block; top:55px; width:105px; background: rgba(255, 255, 255, 0.8); font-size:12px; z-index:100; }
.sub-menu-item { background:url(../images/devider-horizontal.png) no-repeat center top; display:block; height:40px; }
.sub-menu-item:last { background-image:none; display:block; height:40px; }
Can anyone assist me in achieving my goal of making the dropdown menu stay visible when the mouse is still over it?
Upvotes: 0
Views: 1780
Reputation: 15616
you can use setTimeout and a flag check with this.
outside the functions:
var submenuhovered=false;
on hide function:
setTimeout(function(){
if(submenuhovered==false){
$(this).siblings(".sub-menu-items").hide();
}
},200);
on submenu mouseover function
submenuhovered = true;
on submenu mouseout function
submenuhovered = false;
or you can use the menu element's .data() collection instead of the global variable.
$("menu").data("submenuhovered","true");
Upvotes: 0
Reputation: 145368
You can rebuild your code a bit:
$('.sub-menu-items').hide();
$('.menu-item').hover(function(e) {
var el = $(this).children(".menu-text");
el.toggleClass('menu-text-hover');
$(this).toggleClass('menu-item-hover');
if (el.html().indexOf('ABOUT', 0) !== -1)
$(this).find('.sub-menu-items').show('fast');
}, function(e) {
var el = $(this).children(".menu-text");
el.toggleClass('menu-text-hover');
$(this).toggleClass('menu-item-hover');
if (el.html().indexOf('ABOUT', 0) !== -1)
$(this).find('.sub-menu-items').hide('fast');
});
DEMO: http://jsfiddle.net/vnRV4/
The good issue raised @epascarello in the comments. You should better use nested lists instead of div blocks. Moreover, it will be good to replace $('.sub-menu-items').hide()
with CSS style .sub-menu-items { display: none; }
in order to prevent flashing on page load.
Upvotes: 3