悬停在项目上后不显示下拉列表

时间:2016-02-19 09:06:28

标签: html css drop-down-menu

我学习前端几个星期,但我无法弄清楚为什么我的下拉菜单不起作用。 我的主菜单如下:

<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元素的无序列表,在链接中显示div元素关于我们,但它也不起作用。

3 个答案:

答案 0 :(得分:0)

我认为您可以使用onmouseover事件。我做了类似的事情并且工作正常。

@EnableOauth2Sso
@Configuraiton
@EnableWebSecurity
public class SecurityConfig {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().authorizeRequests().anyRequest().authenticated();
  }
}

1)将用户悬停鼠标时显示的div显示为 none

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

答案 1 :(得分:0)

如果您不想使用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标签下移动.dropdown-content 然后你用:

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

jsfiddle

答案 2 :(得分:0)

检查本地测试的代码:

<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>