【问题标题】:Dropdown not displayed after hovering over an item将鼠标悬停在项目上后不显示下拉菜单
【发布时间】:2016-05-31 18:59:25
【问题描述】:

我从几周前开始学习前端,但我无法真正弄清楚为什么我的下拉菜单不起作用。 我的主菜单如下所示:

<div id="main-container">

    <!-- Main navigation -->
    <header class="main-header">
        <div class="header-content">
            <div class="container">

                <!-- Logo -->
                <div class="logo-wrapper pull-left">
                    <a href="index.html">Sit <span>On</span> Chair</a>
                </div>

                <!-- Main menu -->
                <nav class="nav-main pull-right">
                    <ul class="main-nav-list">
                        <li><a href="#" class="dropdown”>About us</a></li>
                        <li><a href="#”>Gallery</a></li>
                        <li><a href="#”>Contact</a></li>
                    </ul>
                    <div class="dropdown-content">
                        <a href="#”>News</a>
                        <a href="#”>Our team</a>
                        <a href="#”>History</a>
                    </div>
                </nav>
            </div>
        </div>
    </header>
</div>

css 文件如下所示:

/* Text styles */
body {
    font-family: 'ralewayregular';
}

@font-face {
    font-family: 'ralewayregular';
    src: url('../fonts/raleway-regular.eot');
    src: url('../fonts/raleway-regular.eot?#iefix') format('embedded-opentype'),
    url('../fonts/raleway-regular.woff2') format('woff2'),
    url('../fonts/raleway-regular.woff') format('woff'),
    url('../fonts/raleway-regular.ttf') format('truetype'),
    url('../fonts/raleway-regular.svg#ralewayregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

h1, h2 {
    text-transform: uppercase;
}

a {
    text-decoration: none;
}

/* Main content */
#main-content {
    position: relative;
    z-index: 0;
}

/* Container styles */
* {
    box-sizing: border-box;
}

.container {
    position: relative;
    width: 1180px;
    margin: 0 auto;
}

.pull-left {
    float: left;
}

.pull-right {
    float: right;
}

/* Main header */
.main-header {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: 99;
    text-transform: uppercase;
    background-color: #000000;
}

/* Logo */
.logo-wrapper {
    margin: 16px 0;
}

/* General header styles */
.header-content .main-nav-list li {
    display: inline;
    padding: 0 30px;
    &:nth-child(3) {
        padding-left: 30px;
        padding-right: 0;
    }
}

.header-content {
    span {
        color: #66dfc9;
        transition: 0.2s ease-in-out;
    }
    a {
        color: #ffffff;
        transition: 0.2s ease-in-out;
        &:hover {
            color: #66dfc9;
            transition: 0.2s ease-in-out;
            span {
                color: #ffffff;
                transition: 0.2s ease-in-out;
            }
        }
    }
}

/* Dropdown */
.dropdown-content {
    position: absolute;
    top: 60px;
    right: 230px;
    width: 140px;
    padding: 10px;
    display: none;
    overflow: auto;
    z-index: 100;
    border-radius: 5px;
    text-transform: none;
    text-align: left;
    background-color: #000000;
    a {
        display: block;
        padding: 10px;
        text-align: center;
        transition: 0.2s ease-in-out;
        &:hover {
            color: #66dfc9;
            transition: 0.2s ease-in-out;
        }
    }
}

.dropdown:hover .dropdown-content {
    display: block;
}

我想在将鼠标悬停在“关于我们”链接上时显示 .dropdown-content。 如果 div 元素在无序列表之外,是否可以显示它? 除了我的代码的最后一部分,一切正常。

我试图更改我的 div 元素的无序列表,在链接 About us 中显示 div 元素,但它也不起作用。

【问题讨论】:

  • 请注意您的 HTML 代码中有一些错误。你有 而不是"
  • 哇,很好,路易斯 - 我什至没看到。
  • @Luis P.A. 好收获。我没有在我的文本编辑器中注意到这一点。谢谢。

标签: html css drop-down-menu


【解决方案1】:

我认为您可以使用 onmouseover 事件。我做了这样的事情,效果很好。

<li><a href="#" class= "dropdown" onmouseover = "document.getElementById('disp').style.display = 'block';">About us</a></li>

<div class="dropdown-content" id = "disp" style="display:none;">

1) 将显示设置为 none 以显示您希望在用户悬停鼠标时显示的 div

2) 对于“关于我们”链接,添加事件“onmouseover”并将 div 显示设置为“阻止”。

【讨论】:

  • 它也可以正常工作。我只需要添加关闭下拉列表的事件,即单击下拉列表之外的任何位置时,因为在这种情况下,下拉列表会显示,直到页面刷新/重新加载,但我知道该怎么做。谢谢
【解决方案2】:

如果你不想使用 javascript,下面的代码可以工作:

HTML

 <!-- Main menu -->
            <nav class="nav-main pull-right">
                <ul class="main-nav-list">
                    <li><a href="#" class="dropdown">About us</a>
                     <div class="dropdown-content">
                    <a href="#">News</a>
                    <a href="#">Our team</a>
                    <a href="#">History</a>
                </div></li>
                    <li><a href="#">Gallery</a></li>
                    <li><a href="#">Contact</a></li>
                </ul>

            </nav>

您必须将 .dropdown-content 移动到 .dropdown 标记下 然后你选择它使用:

.dropdown:hover + .dropdown-content {
    display: block;
}

jsfiddle

【讨论】:

  • 我自己尝试将 .dropdown-content 移动到 .dropdown 标签下,但选择器 .dropdown:hover .dropdown-content 不起作用。谢谢你的帮助。
  • 您必须使用 + 选择器才能完成这项工作@Kamil
  • 否则规则 .dropdown:hover .dropdown-content 会寻找一个名为 .dropdown-content 的 .dropdown 后代,但它并不存在
  • 是的,你是对的。我没有使用 + 选择器,这是我的小错误。
【解决方案3】:

检查此代码在本地测试:

<style type="text/css">
            ul{
                background: gray;
            }
            ul>li{
                padding: 10px 15px;
                font-size: 14px;
                font-family: Arial;
                display: inline-block;
                cursor: pointer;
            }
            ul li.dropdown{
                position: relative;
            }
            .dropdown-list{
                position: absolute;
                display: none;
                width: 80px;
            }
            ul li.dropdown:hover > .dropdown-list{
                display: block;
            }
        </style>
        <ul>
            <li>Home</li>
            <li class="dropdown">About
                <ul class="dropdown-list">
                    <li>Item-1</li>
                    <li>Item-2</li>
                    <li>Item-3</li>
                    <li>Item-4</li>
                </ul>
            </li>
            <li>Contact</li>
        </ul>

【讨论】:

  • 我什至不需要在本地检查它,因为我认为它会起作用:) 这是使用无序列表和列表项的经典下拉菜单。我想知道是否可以使用 ul、li 和 div 标签来创建它。无论如何感谢您的帮助
猜你喜欢
  • 2013-03-14
  • 1970-01-01
  • 2019-12-26
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 2014-03-18
相关资源
最近更新 更多