3

I'm not sure how to phrase this to be honest.

I have a navigation bar which exposes sub menus when hovered lets say

#navigation:hover #submenu {visibility: visible} 

within the submenu I have a close button which I when clicked should stop the hover on the navigation element. But not remove it for future hovers.

How would I get around this problem ?

5
  • So it should collapse the submenu? Commented Dec 22, 2014 at 16:23
  • 1
    Can you post your html and CSS? Commented Dec 22, 2014 at 16:23
  • for manage this type of interaction you should manage via javascript. if you post a fiddle is easier understand the structure. Commented Dec 22, 2014 at 16:24
  • Here is a fiddle of what I am trying to achieve. jsfiddle.net/v8jroxvx sorry for the vague nature of the question, I'm not very sure on the jargon to use x Commented Dec 22, 2014 at 16:25
  • So are you trying to achieve: Upon hovering the navigation bar, the links are displayed, when your mouse leaves the navigation the navigation is still visible however, clicking close. Closes it? Commented Dec 22, 2014 at 16:35

3 Answers 3

1

Use :active selector.

#test:hover #grapes {
    display: block;
}
#grapes {
    display: none;
    border: 1px solid black;
    padding-bottom: 2em;
    position: relative;
}
#fubar {
    position: absolute;
    bottom: 0px;
}
#fubar:active + ul {
    display: none;
}

Working Fiddle

PS: I interchanged the ul and #fubar elements' positions to get desired solution using adjacent sibling selector.

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting, what about compatibility ? Is this working great in IE 8-9 ?
@enguerranws yup.. should work as I am not using css3 here. :)
Yes but IE and some others don't trigger / understand :active pseudo-class on all html elements.
@enguerranws works fine for IE8+ MDN Link
0

In this case, being explicit is the answer. hover and toggleFade are useful shortcuts to things, but when you need to do something more complex (like have a manual intervention in the hover or fade handling) then you're better using an explicit, expanded form, like this: http://jsfiddle.net/v8jroxvx/1/

$('#test').on('mouseenter', function(){
    $('#grapes').stop(true, true).fadeIn();
}).on('mouseleave', function() { $('#grapes').stop(true, true).fadeOut(); })

$('#fubar').click(function()
{
    $('#grapes').fadeOut();
})

I am explicitly saying on mouse enter, fade the nav in and on mouse leave, fade the nav out and in the case of clicking the close button, I am explicitly saying when I clicick #grapes, forcefully fade the nav out.

Comments

0

I do something like this

    $hoveredContainer.css('pointer-events', 'none');
    setTimeout(function () {
        $hoveredContainer.css('pointer-events', 'auto')
    }, 10);

Where $hoveredContainer is a variable containing the jQuery element that is being hovered.

What that code does is "unhover" the element for a split second.

It works for drop downs when the mouse is over the 'expanded' area where the selection is being made.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.