HTML+CSS导航栏自适应方案

css文件


#header {
    background-color: #333;
    padding: 10px 10%;
}

/* 导航栏按钮 */
.btn {
    display: none;
    float:right;
    margin-top: 5px;
    border: 0;
    border-radius: 5px;
    width: 40px;
    height: 40px;
}
/* 文字Logo */
#header a.logo {
    float:left;
    font-weight: bold;
    font-size: 39px;
    line-height: 50px;
}

#header div.nav {
    height: 50px;
}

#header ul.nav-bar {
    list-style: none;
    float: right;
    line-height: 50px;
}
#header ul.nav-bar li {
    float:left;
    padding: 0 10px;
}

@media screen and (max-width: 768px){
    #header {
        padding: 10px 5%;
        height: 50px;
        overflow: hidden;
    }
    #header a.logo {
        line-height: 55px;
        font-size: 30px;
    }
    .btn {
        display: block;
    }
    #header ul.nav-bar {
        margin-top: 15px;
        border-top: solid 1px #111;
        width:100%;
    }
    #header ul.nav-bar li {
        float: none;
        padding: 0 10px;
    }
}

#container {
    width: 100%;
    height: 300px;
    background:#FFBBBB;
}

html文件


<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
    <title>title>
    <link rel="stylesheet" type="text/css" href="./style.css">
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">script>
head>
<body>

<div id="header">
    <div class="nav">
        <a href="#" class="logo">自适应导航栏a>
        <div id="nav-btn" class="btn">
            <img src="./menu.png" style="margin: 5px;width: 30px;height:30px;" />
        div>
        <ul class="nav-bar">
            <li><a href="#">首页a>li>
            <li><a href="#">归档a>li>
            <li><a href="#">标签a>li>
            <li><a href="#">关于a>li>
        ul>
    div>
div>

<div id="container">
    <h3>正文内容Contenth3>
div>

body>
<script type="text/javascript">
        // 开关状态
    var open = false;
    $('#nav-btn').click(function (){
                // 按钮状态
        $(this).css("background-color", open ? '#333' : '#222');
        var navBar = $('.nav-bar');
                // 设置header的高度,将导航列表显示出来
        var height = navBar.offset().top + navBar.height();
        $('#header').animate({
            height: open ? 50 : height
        });
                // 修改开关状态
        open = !open;
    });

script>
html>

你可能感兴趣的:(css,html,css)