wyc
wyc

Reputation: 55273

How can I make it so the dropdown menu stays there when I'm hovering on it?

I have the following dropdown menu:

<div id="header">
  <div class="container">
    <ul class="navigation">
      <li>
        <a href=""></a>
        <ul class="sub-menu">
          <li></li>
        </ul>
      </li>
    </ul>
  </div>
</div>

#header .navigation {
    float: right;
    margin: 15px 0 0;
    position: relative;
}
#header .navigation li {
    float: left;
    margin: 0 20px 0 0;
    position: relative;
}
#header .navigation li a {
    float: left;
}
#header .navigation li:hover .sub-menu {
    display: block;
    transition: all 0.0s ease 0s;
}
#header .navigation > li > .sub-menu {
    display: none;
    width: 150px;
    transition: all 0.0s ease 0.2s;
    padding: 10px 15px 2px;
    position: absolute;
    top: 20px;
    left: 0;
}
#header .navigation > li > .sub-menu li {
    clear: both;
    margin: 0 0 10px;
    width: 100% ;
}
#header .sub-menu .sub-menu li {
    padding: 10px 0 0 15px;
    margin: 0 !important;
}

The problem is that the .sub-menu disappears immediately after I leave the parent. How can I make it so that .sub-menu stays where it is as long as I'm hovering it?

Here's a live example: http://m2c.dreamhosters.com/wordpress/

Upvotes: 0

Views: 47

Answers (2)

albusshin
albusshin

Reputation: 4010

It seems to me that the problem lies on this line of code:

top: 20px;

If you do a very fast move of your mouse from the list item anchor tag to the .sub-menu, the .sub-menu won't disappear.

So what you've gotta do is to reduce the gap from a to .sub-menu.

For example you can change top: 20px; to padding-top: 20px;, it would do the trick. However the style might change.

UPDATE: For the link you provided, you can simply change top: 20px; to top: 15px.

Upvotes: 0

ralph.m
ralph.m

Reputation: 14345

Two things to note:

  • place any padding on the links themselves, as you don't want any gaps between the top li and the sub uls
  • make sure the sub ul is neatly tucked at the bottom of the top li, with top: 100%. (That prevents any gap between them.

E.g.

#header .navigation {
    float: right;
    margin: 15px 0 0;
    position: relative;
}
#header .navigation li {
    float: left;
    margin: 0 20px 0 0;
    position: relative;
}
#header .navigation li a {
    float: left;
}
#header .navigation li:hover .sub-menu {
    display: block;
    transition: all 0.0s ease 0s;
}
#header .navigation li .sub-menu {
    display: none;
    width: 150px;
    transition: all 0.0s ease 0.2s;
    position: absolute;
    top: 100%;
    left: 0;
}
#header .navigation > li > .sub-menu li {
    clear: both;
    width: 100%;
}


ul, li {list-style: none; padding: 0; margin: 0;}
<div id="header">
<ul class="navigation">
  <li>
    <a href="">link</a>
    <ul class="sub-menu">
      <li>text</li>
    </ul>
  </li>
</ul>
</div>

Upvotes: 2

Related Questions