响应式菜单下拉菜单未显示

时间:2014-05-23 21:20:19

标签: javascript html css menu

我正在努力使我的菜单响应,但它的工作不正常。

菜单更改大小等,但在按“导航按钮”时不显示菜单下拉列表。

你可以帮帮我吗? :)

CSS

/* #Base NAV 960 Grid
================================================== */       

        nav {
            padding-top: 10px;
            text-align:center;
            margin: 0 auto;
        }

        nav ul {
            display: inline;
            list-style-type: none;
            padding-left: 0px;  
        }

        nav li {
            display: inline;
            padding-left: 55px;
            padding-right: 55px;
            font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
            text-transform: uppercase;
            font-weight: 300;

        }

        nav li a:hover { 
            color:#808080;
            -webkit-transition: all 0.3s ease-in-out;
        }


        nav li a {
            color: #282828;
            text-decoration: none;
            font-size: medium;
        }

        .nav-button { display: none; }

        .logoo { display: none; }
    }

@media only screen and (min-width: 768px) and (max-width: 959px) {
        nav li {
            padding-left: 20px;
            padding-right: 20px;
        }       
}


@media only screen and (min-width: 0px) and (max-width: 767px) {

    /* Navigation Button
    -------------------------------------------------------- */

    nav ul img {
        display: none;
    }

    .logoo {
        display: inline;
        width: 150px;
        margin-top: 0px;
        margin: 0 auto;
    }

    .nav-button {
        display: block;
        position: absolute;
        top: 7px;
        left: 7px;
        width: 50px;
        height: 35px;
        background: url('../images/menu-icon-large.png'), -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background: url('../images/menu-icon-large.png'),    -moz-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background: url('../images/menu-icon-large.png'),     -ms-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background: url('../images/menu-icon-large.png'),      -o-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background-position: center center;
        background-repeat: no-repeat;
        background-size: 21px, 100%;
        cursor: pointer;
        border: 0 none;
        border-bottom: 1px solid rgba(255,255,255,.1);
        box-shadow: 0 0 4px rgba(0,0,0,.7) inset;
        border-radius: 5px;
        z-index: 999;
        text-indent: -9999px;
    }
    .nav-button:hover { 
        background-color: rgba(0,0,0,.1); 
    }
    .nav-button.open {
        background: url('../images/close-icon-large.png'), -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background: url('../images/close-icon-large.png'),    -moz-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background: url('../images/close-icon-large.png'),     -ms-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background: url('../images/close-icon-large.png'),      -o-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background-position: center center;
        background-repeat: no-repeat;
        background-size: 21px, 100%;
    }

    /* Navigation Bar
    -------------------------------------------------------- */

    body { padding-top: 50px; } 

    nav {
        width: 100%;
        float: none;
        background-color: #b0b0b0; /* change the menu color */
        background-image: -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background-image:    -moz-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background-image:     -ms-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        background-image:      -o-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,.2));
        display: block;
        height: 50px;
        margin: 0;
        padding: 0;
        overflow: hidden;
        box-shadow: 0 1px 2px rgba(0,0,0,.6);
        position: absolute;
        left: 0px;
        z-index: 998;
        clear: both;
    }
    nav li {
        display: none;
        width: 100%;
    }
    nav li a {
        display: block;
        width: 90%;
        padding: 10px 5%;
        font-size: 14px;
        font-weight: bold;
        text-shadow: -1px -1px 0 rgba(0,0,0,.15);
        color: white;
        text-decoration: none;
        border-bottom: 1px solid rgba(0,0,0,.2);
        border-top: 1px solid rgba(255,255,255,.1); 
    }
    nav li a:hover {
        background-color: rgba(0,0,0,.5);
        border-top-color: transparent;
    }
    nav > li:first-child {
        border-top: 1px solid rgba(0,0,0,.2);
    }

    /* Toggle the navigation bar open  */

    nav.open { 
        height: auto; 
        padding-top: 50px;
    }
    nav.open li { 
        display: block; 
    }

    /* Submenus – optional .parent class indicates dropdowns */

    nav > li:hover > a {
        background: rgba(0,0,0,.5);
        border-bottom-color: transparent;
    }
    nav li.parent > a:after {
        content: "▼";
        color: rgba(255,255,255,.5);
        float: right;
    }
    nav li.parent > a:hover {
        background: rgba(0,0,0,.75);
    }
    nav li ul {
        display: none;
        background: rgba(0,0,0,.5);
        border-top: 0 none;
        padding: 0;
    }
    nav li ul a {
        border: 0 none;
        font-size: 12px;
        padding: 10px 5%;
        font-weight: normal;
    }
    nav li:hover ul {
        display: block;
        border-top: 0 none;
    }

}

JS

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<meta name="viewport" content="width=device-width">
<script>


$(document).ready(function(){
    $(".nav-button").click(function () {
    $(".nav-button,.primary-nav").toggleClass("open");
    });


});
</script>

HTML

    <header>
    <a href="index.php"><img class="logoo" src="images/logo.png"></a>
    <nav>
        <button class="nav-button">Toggle Navigation</button>
        <ul>
            <li> <a href="index.php"> home </a> </li>
            <img src="images/dott.png" width="8px">
            <li> <a href="about.php"> about </a> </li>
            <a href="index.php"> <img src="images/logo.png" alt="Logo" width="180px"> </a>
            <li> <a href="portfolio.php"> portfolio </a> </li>
            <img src="images/dott.png" width="8px">
            <li> <a href="#" target="_blank"> blog </a> </li>

        </ul>

    </nav>

</header>

1 个答案:

答案 0 :(得分:1)

你似乎错过了&#34; primary-nav&#34;在您的元素上,您当前正在选择jQuery和样式。

将您的CSS更改为:

.primary-nav.open { 
    height: auto; 
    padding-top: 50px;

}
.primary-nav.open li { 
    display: block; 
}

和你的JS:

$(document).ready(function(){
$(".nav-button").click(function () {
    $(".primary-nav").toggleClass("open");
});

最后,将课程添加到<nav>

<nav class="primary-nav">