CSS:针对移动菜单隐藏了溢出

时间:2018-06-30 01:19:29

标签: jquery css css3

我致力于响应式导航。现在,一切正常,响应迅速。我希望将移动菜单固定在全高移动设备上,因此尝试在媒体上的类overflow:hidden;上使用.nav ul,还尝试使用jQuery if($('.menu-icon').is(':visible'))添加position:fixed;,但是我做错了。

body, html {
    margin: 0;
    padding: 0;
    width: 100%;
}

body {
    font-family: Quicksand;
}

header {
    width: 100%;
    height: 100vh;
    background: url("http://www.idizajner.com/dev/img/header.jpg") no-repeat 50% 50%;
    background-size: cover;
}

.logo {
    position: fixed;
    float: left;
    margin: 16px 36px;
    color: #fff;
    font-weight: bold;
    font-size: 24px;
}

nav {
    position: fixed;
    width: 100%;
}

nav ul {
    list-style-type: none;
    background: rgba(0,0,0,0);
    overflow: hidden;
    color: #fff;
    padding: 0;
    text-align: center;
    margin: 0;
    transition: 1s;
}

nav.black ul {
    background: #000;
}

nav ul li {
    display: inline-block;
    padding: 20px;
}
nav ul li a {
    text-decoration: none;
    color: #fff;
    font-size: 20px;
}

.menu-icon {
    width: 100%;
    background: #000;
    text-align: right;
    box-sizing: border-box;
    padding: 15px 24px;
    cursor: pointer;
    color: #fff;
    display: none;
}

@media(max-width: 580px) {
    .logo {
        position: fixed;
        top: 0;
        margin-top: 16px;
    }
    nav ul {
        max-height: 0px;
        background: #000;
        height: 100vh;
    }
    nav.black ul {
        background: #000;
    }
    .showing {
        max-height: 100vh;
    }
    nav ul li {
        box-sizing: border-box;
        width: 100%;
        padding: 24px 0;
        text-align: center;
    }
    .menu-icon {
        display: block;
    }
}
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href=""><!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Navbar</title>
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    
    <script type="text/javascript">
// Menu toogle-button
$(document).ready(function () {
$(".menu-icon").on("click", function () {
    $("nav ul").toggleClass("showing");
})
})

//Scrolling effect

$(window).on("scroll", function() {
if($(window).scrollTop()) {
    $('nav').addClass('black');
}
else {
    $('nav').removeClass('black');
}
})
</script>

</head>

<body>
    


<div class="wrapper">
    <header>
        <nav>
            <div class="menu-icon">
                <i class="fa fa-bars fa-2x"></i>
            </div>
            <div class="logo">
                LOGO
            </div>
            <div class="menu">
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Contact</a></li>
                </ul>
            </div>


            
        </nav>
    </header>

    <section>
    <p>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/>Text text text<br/></p>
    </section>
    
</div>
    


</body>
</html>

这是我的代码段。以全角打开并调整浏览器大小以选择汉堡菜单。谢谢!

1 个答案:

答案 0 :(得分:0)

您尚未清楚说明问题。但是,据我检查您的代码后,我发现:

  • 您在代码中指定了height: 100vh,这会导致标头占据视口的整个高度。因此,剖面内容在视口之外。尝试给定固定高度,例如height: 50px;
  • 与上述相同,您将为汉堡菜单列表提供完整的视口高度。给height: 100%很好。
  • 滚动时不会隐藏并显示nav。只是改变背景。如果您不想在滚动之前显示菜单,请隐藏菜单,然后在滚动时display: block隐藏。这样,您的栏目内容可以占用标题空间,并避免页面中出现空白。