切换导航栏的问题

时间:2015-12-08 01:57:56

标签: html css

好的,目前我有两个导航栏,一个用于桌面/大型平板电脑,另一个用于移动视图。当我达到一定的尺寸时,我不知道如何切换它。这是我的尝试,但它没有用。



body, html {
    margin: 0;
    font-family: sans-serif, arial;
}

.content {
    padding: 30px;
}

.nav-main-mobile {
    display: none;
}

.nav-main {
  width: 100%;
    background-color: #222;
    height: 70px;
    color: #fff;
}

.nav-main .logo {
    float: left;
    height: 40px;
    padding: 15px 30px;
    font-size: 1.4em;
    line-height: 40px;
}

.nav-main > ul {
    margin: 0;
    padding: 0;
    float: left;
    list-style-type: none;
}

.nav-main > ul > li {
    float: left;
}

.nav-item {
    display: inline-block;
    padding: 15px 20px;
    height: 40px;
    line-height: 40px;
    color: white;
    text-decoration: none;
}

.nav-item:hover {
    background-color: #444;
}


.nav-content {
    position: absolute;
    top: 70px;
    overflow: hidden;
    background-color: #222;
    max-height: 0;
}

.nav-content a {
    color: #fff;
    text-decoration: none;
}

.nav-content a:hover {
    text-decoration: underline;
}

.nav-sub {
    padding: 20px;
}

.nav-sub ul {
    padding: 0px;
    margin 0px;
    list-style: none;
}

.nav-sub ul li a {
    display: inline-block;
    padding: 5px 0px;
}

.nav-item:focus {
    background-color: #444;
}

.nav-item:focus ~ .nav-content {
    max-height: 400px;
    -webkit-transition: max-height 0.4s ease-in;
    -moz-transition: max-height 0.4s ease-in;
    transition: max-height 0.4s ease-in;
}

@media screen and (max-width: 580px) {
    .nav-main {
        display: none;
    }

    .nav-main-mobile {
        display: all;
    }
}

<!DOCTPYE html>
<html>
<head>
    <meta charset="utf-8" />
		<title>Learn HTML and CSS</title>
		<link rel="stylesheet" href="app.css" />
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
	<body>
        
        <nav class="nav-main">
            
            <ul>
                <li>
                    <a href="index.html" class="nav-item">Home</a>
                </li>
                
                <li>
                    <a href="#" class="nav-item">Materials <span class="glyphicon glyphicon-menu-down" aria-hidden="true"></span></a>
                    <div class="nav-content">
                        <div class="nav-sub">
                            <ul>
                                <li><a href="#">Text-Editors</a></li>
                                <li><a href="#">Web Browsers</a></li>
                            </ul>
                        </div>
                    </div>
                </li>
                
                <li>
                    <a href="#" class="nav-item">Getting Started <span class="glyphicon-menu-down" aria-hidden="true"></span></a>
                    <div class="nav-content">
                        <div class="nav-sub">
                            <ul>
                                <li><a href="#">Setting It Up</a></li>
                                <li><a href="#">HTML</a></li>
                                <li><a href="#">CSS</a></li>
                            </ul>
                        </div>
                    </div>
                </li>
                
                <li>
                    <a href="#" class="nav-item">Other</a>
                    <div class="nav-content">
                        <div class="nav-sub">
                            <ul>
                                <li><a href="#">Sign Up</a></li>
                                <li><a href="#">Sign In</a></li>
                                <li><a href="#">Sources</a></li>
                            </ul>
                        </div>
                    </div>
                </li>
            </ul>
        </nav>

        <nav class="nav-main-mobile">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>
                <li><a href="#">stuff</a></li>                
            </ul>    
        </nav>
        
        <div class="content">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.      Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. 

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. 

Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. 

Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. 


        </div>
    </body>
</html>
&#13;
&#13;
&#13;

提前谢谢你。

2 个答案:

答案 0 :(得分:1)

它是display: block,而不是display: all

答案 1 :(得分:0)

您必须使用media queries来告诉浏览器您希望它为每个特定实例应用哪些CSS规则。

这是一个非常简单的小提琴,其中包含最大宽度为500px的设备的媒体查询,其中我将导航背景颜色从原始黑色覆盖为红色。

/* original class */
nav{
  height:50px;
  width:100%;
  background:#000;
}

/* when the window size is max 500px apply this rules */
@media (max-width:500px){
  nav{
    background:red;
  }
}

http://jsfiddle.net/vxyoz78x/

希望有所帮助。