簡體   English   中英

使用香草JavaScript向下滾動時隱藏菜單,向上滾動時顯示

[英]Hide menu when scroll down and show when scroll up by using vanilla JavaScript

我有一個導航菜單。 這個菜單,我已經用bootstrap構建了,我希望它在向下滾動時隱藏並在向上滾動時顯示。 我僅通過使用普通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;
}

就像本文建議的那樣,“ 自動隱藏粘性標題” (我也用這4個單詞在Google中立即找到了解決方案),您可以將標題設置為固定

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

然后對滾動做出反應,以使用JS更改top屬性。

祝好運'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM