悬停后保持下拉菜单打开(CSS) [英] Keep drop down menu open after hover (CSS)

查看:102
本文介绍了悬停后保持下拉菜单打开(CSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个水平菜单,当您将鼠标悬停在某个菜单上时,将显示一个下拉菜单.一切都很好.但是,当您离开菜单项(使用下拉菜单)时,下拉菜单将消失.我了解这是因为您不再将鼠标悬停在上面,但是我该如何解决呢?注意:我不希望下拉菜单位于其正下方,我希望菜单项和下拉菜单之间有合理的距离(因为目前有).谢谢.

I have created a horizontal menu that when you hover an item, a drop down menu appears. This is all fine. However, when you leave the menu item (to use the drop down) the drop down disappears. I understand that this is because you are no longer hovering it, but how do I solve this? Note: I don't want the drop down menu directly below it, I want a reasonable gap between the menu item and drop down (as I have it at the moment). Thanks.

HTML

<header id="header">
    <div class="container">
        <a href="#top-anchor"><div id="logo"></div></a>
        <nav class="header-menu">
            <a href="index.html" class="header-menu-item">ABOUT</a>
                <div class="about-dropdown">
                    <a href="index.html#core-services-anchor">CORE SERVICES</a>
                    <a href="index.html#atandl-anchor">AT&amp;L</a>
                    <a href="index.html#hseq-anchor">HSEQ</a>
                    <a href="index.html#clients-anchor">CLIENTS</a>
                    <a href="index.html#contact-anchor">CONTACT</a>
                </div>
            <a href="services.html" class="header-menu-item">SERVICES</a>
            <a href="facilities.html" class="header-menu-item">FACILITIES</a>
            <a href="#map-anchor" class="header-menu-item">CONTACT</a>
        </nav>
        <div id="hamburger"></div>
        <!--<div id="box-shadow-menu"></div>-->
    </div>
</header>

CSS

#header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100px;
  user-select: none;
  display: block;
  transition: all 0.8s;
  line-height: 100px;
  z-index: 1000;
  transform: translateX(0);
  backface-visibility: hidden;
  margin: 0;
}

header .container {
  width: 1440px;
  height: 100px;
  border-bottom: 0.75px solid rgba(255,255,255,0.1);
}

#logo {
  width: 55px;
  height: 55px;
  float: left;
  margin-top: 27px;
  background-image: url(../images/logo_white.png);
  background-size: cover;
}

nav.header-menu {
  float: right;
  height: 96px;
  vertical-align: middle;
  padding-top: 1px;
}

.header-menu-item {
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  font-weight: 800;
  margin-left: 20px;
  text-decoration: none;
  color: #ffffff;
  line-height: 96px;
  letter-spacing: 0.5px;
  transition: 0.55s;
}

.toggle {
  opacity: 0.3;
}

.current {
  border-bottom: 2px solid #fff;
  padding-bottom: 40px;
}

.about-dropdown {
  position: absolute;
  background-color: #fff;
  min-width: 160px;
  box-shadow: 0 0 4px 3px rgba(0,0,0,0.1);
  z-index: 3000;
  margin-top: 35px;
  margin-left: -35px;
  border-radius: 3px;
  display: none;
  transition: 0.8s;
}

.about-dropdown a {
  display: block;
  text-decoration: none;
  padding: 0px 28px;
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  font-weight: 800;
  margin: 0;
  line-height: 50px;
  border-bottom: 1px solid rgba(0,0,0,0.1);
}

.header-menu-item:hover + .about-dropdown {
  display: block;
}

推荐答案

在'a'标记上,悬停时在其上添加高度或填充底部.您的'a'标签可能需要绝对放置,以使其高度不会影响标头的高度.

On the 'a' tag, add a height or padding-bottom to it on hover. Your 'a' tag might need to be positioned absolute so that its height won't affect the height of your header.

类似以下内容

.about-dropdown a:hover {
    padding-bottom: 30px; /*height dependent on the gap you want to fill*/
    position: absolute; 
}

这篇关于悬停后保持下拉菜单打开(CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆