向下滚动时隐藏菜单,使用vanilla JavaScript向上滚动时显示

时间:2015-07-29 10:40:41

标签: javascript html css twitter-bootstrap

我有一个导航菜单。这个菜单,我用bootstrap构建它,我想让它在向下滚动时隐藏,并在向上滚动时显示。我只使用vanilla JavaScript获得了它的要求。

这是我的HTML代码:

<nav class="navbar navbar-default navbar-fixed-top  custom-header" >
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Menu</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <a class="navbar-brand" href="" style="color:#FFF">
                    <img src="img/header-logo.png" alt="Happy Tour" />
                </a>
            </div>

            <!-- This section contains all link buttons in the header -->
            <div class="collapse navbar-collapse pull-right mybtn" id="bs-example-navbar-collapse-1" style="margin-top:9px;">
                <button class="btn btn-primary btn-md"><i class="glyphicon glyphicon-bookmark"></i> Destinations</button>
                <button class="btn btn-success btn-md"><i class="glyphicon glyphicon-th"></i> Tour Package</button>
                <button class="btn btn-danger btn-md"><i class="glyphicon glyphicon-file"></i> Book Ticket</button>
                <button class="btn btn-info btn-md"><i class="glyphicon glyphicon-search"></i> Find Hotel</button>
                <button class="btn btn-success btn-md"><i class="glyphicon glyphicon-user"></i> Register</button>
                <button class="btn btn-primary btn-md"><i class="glyphicon glyphicon-fire"></i> Login</button>
            </div>
        </div>
    </nav>

这是一个css:

.custom-header{
     padding: 0;
     height: 120px;
     line-height: 120px;
     background: url(../img/top_head_bg.jpg) repeat-x;
     border:none;
}

1 个答案:

答案 0 :(得分:0)

与本文Auto-Hide Sticky Header建议一样(我也使用这4个字在 Google 中立即找到解决方案),您可以将标题设置为固定< / p>

.header {
    width: 100%;
    height: 7.5em;
    position: fixed;
    z-index: 1000;
    top: 0;
    left: 0;
}

然后对滚动做出反应,使用 JS 更改top属性。

祝你好运&#39;