0

我正在创建一个响应式导航菜单,除了我的页面内容的其余部分在菜单展开时没有被按下之外,一切都运行良好。

我进行了一些更改,以便响应式宽度显示在 JSFiddle 中——结果有些事情会消失,但您将能够了解它的要点。

任何帮助将非常感激。

JSFiddle:http: //jsfiddle.net/hxN5U/1/

html:

<div id="wrapper">
    <a id="logo_link" href="#" target="_blank"></a>
    <div id="header"></div>

    <div id="nav" class="clearfix">
        <ul class="clearfix">
            <li><a href="index.html" class="current_page">Home</a></li>
            <li><a href="search.php">Search</a></li>
            <li><a href="submit.html">Submit</a></li>
            <li><a href="#" target="_blank">News</a></li>
            <li><a href="about.html">About</a></li>
            <li><a href="contact.html">Contact</a></li>
        </ul>

        <a href="#" id="pull">Menu</a>
    </div>


    <div id="content">

        <div id="main">

        </div>


        <div id="footer">
            <hr />
            <div id="footer_content">
                <div id="sm_icons">
                    <a href="#" target="_blank"><img src="img/icon_facebook.png"  style="height:30px; width:30px;" alt="Facebook"/></a>
                    <a href="#" target="_blank"><img src="img/icon_twitter.png" style="height:30px; width:30px;" alt="Twitter"/></a>
                    <a href="#" target="_blank"><img src="img/icon_linkedin.png" style="height:30px; width:30px;" alt="LinkedIn"/></a>
                    <a href="#" target="_blank"><img src="img/icon_youtube.png" style="height:30px; width:30px;" alt="YouTube"/></a>
                </div>

                <div id="copyright">
                    <strong>© 2013</strong>
                </div>
            </div>

        </div>

    </div>
</div>

CSS:

div#nav {
    height: 40px;
    width: 100%;
    position: relative;


    background-repeat: repeat-x;
    background-position: 25px;

    background-color: #003A70;
    -moz-box-shadow: 0 0 8px #666;
    -webkit-box-shadow: 0 0 8px #666;
    box-shadow: 0 0 8px #666;
}

#nav ul{
    margin: 0 auto;
    height: 40px;
    width: 960px;
    padding: 0;
}

#nav li {
    display: inline;
    float: left;
}

#nav a {
    display: inline-block;
    width: 160px;
    text-align: center;
    line-height: 40px;
    font-family: 'Quattrocento Sans', Helvetica, "Trebuchet MS", Arial, sans-serif;
    font-weight: bold;
    font-size: 15px;
    color: #fff;
    text-decoration: none;
}

#nav li a {
    box-sizing:border-box;  
    -moz-box-sizing:border-box;  
    -webkit-box-sizing:border-box; 
    border-right: 1px solid #ccc;
}

#nav li:last-child a { 
    border-right: 0;
}


#nav ul a:hover, #nav .current_page {
    background-image: url('../img/nav_selected_bg.jpg');
    color: #FFFFFF;
}

#nav a#pull {
    display: none;
}

body {
    margin: 0;
    padding: 0;
    background-image: url('../img/main_bg.png');
}

h1 {
    font-family: 'Helvetica';
    color: #971e23;
    text-align: center;
    font-size: 18px;
}

h2 {
    font-family: 'Helvetica';
    color: #000;
    text-align: center;
    font-size: 16px;
}

p {
    font-family: 'Arial';
    font-size: 14px;
    line-height: 1.5em;
}

div#wrapper {
    max-width: 960px;
    margin: 0 auto;
    margin-bottom: 25px;
    -moz-box-shadow: 0 0 8px #666;
    -webkit-box-shadow: 0 0 8px #666;
    box-shadow: 0 0 8px #666;
}


div#header {
    height: 75px;
    background-image: url('../img/top_banner.jpg');
    background-color: #f3f1f2;
    background-repeat:no-repeat;
    background-size:contain;
    background-position:center;
}

div#footer {
    width: 100%;
    margin: 0;
    background-color: #f3f1f2;
    clear: both;
}

div#footer_content {
    padding: 0px 25px 50px 25px;
}


div#footer hr {
    margin-top: 0; 
    padding:0;
    border: 0;
    border-bottom: 1px solid #ccc;
    background: #999;
}


div#sm_icons {
    width: 135px;
    float: left;
    margin-top: -7px;
}

div#copyright {
    width: 90%;
    text-align:center;
    margin-top: 40px;
    font-size: 12px;
}

@media only screen and (max-width: 600px) {
    div#header {
        background-image: url('../img/top_banner_small.jpg');
    }

    div#copyright {
        clear: both;
    }

    div#sm_icons {
        float: none;
        width: 135px;
        margin: 0 auto;
        margin-top: 20px;
    }
}

@media only screen and (max-width: 600px) {

    #wrapper, #content, #footer, #main, #nav {
        height: auto;
    }

    div#header {
        background-image: url('../img/top_banner_small.jpg');
    }

    #nav ul {
        display: none;
        height: auto;
    }

    #nav li {
        display: block;
        float: none;
        width: 100%;
    }

    #nav li a {
        border-bottom: 1px solid #ccc;
            background-color: #003A70;
    }

    #nav a#pull {
        display: block;
        width: 100%;
        position: relative;
        background-repeat: repeat-x;
        background-position: 25px;
            background-color: #003A70;
    }

    #nav a#pull:after {
        content: "";
        background: url('../img/nav-icon.png') no-repeat;
        display: inline-block;
        width: 30px;
        height: 30px;
        position: absolute;
        right: 15px;
        top: 10px;
    }
}

jQuery:

jQuery(function ($) {
var menu = $('#nav ul');
var menuHeight  = menu.height(); 

$('#nav a#pull').on('click', function(e){
    e.preventDefault();
    menu.slideToggle();
});

$(window).resize(function(){  
    var w = $(window).width();  
    if(w > 320 && menu.is(':hidden')) {  
        menu.removeAttr('style');  
    }  
});  

});
4

1 回答 1

1

http://jsfiddle.net/hxN5U/2/去掉#nav的高度

div#nav {
    /*height: 40px;*/
    width: 100%;
    position: relative;
    background-repeat: repeat-x;
    background-position: 25px;
    background-color: #003A70;
    -moz-box-shadow: 0 0 8px #666;
    -webkit-box-shadow: 0 0 8px #666;
    box-shadow: 0 0 8px #666;
}
于 2013-11-12T20:00:25.317 回答