简体   繁体   English

如何使导航栏出现在某个部分?

[英]How can I make my navbar appear at a certain section?

Problem: 问题:

How can I make my navigation not appear on my #home section. 如何使导航不出现在#home部分中。 However, when the user scrolls down or clicks on the find more button. 但是,当用户向下滚动或单击“查找更多”按钮时。 When the user gets to the 'features-icons' section how can I make my navigation bar appear for all sections including that and below? 当用户进入“功能图标”部分时,如何使我的导航栏出现在包括该部分和以下部分的所有部分中? https://github.com/ldocherty1/Unit28_Assignment1 https://github.com/ldocherty1/Unit28_Assignment1

What I have tried: 我尝试过的

HTML 的HTML

<nav class="navbar scrolled-nav fixed-top navbar-expand-sm bg-light">
    <a class="navbar-brand" href="#">Front End Web Developer</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-collapse">☰</button>
    <div class="collapse navbar-collapse" id="navbar-collapse">
        <ul class="nav navbar-nav ml-auto">
            <li class="nav-item active"> <a class="nav-link" href="#home">Home</a>
            </li>
            <li class="nav-item">
                <a class="nav-link active" href="#features-icons">Expectations</a>
            </li>
            <li class="nav-item active"> <a class="nav-link" href="#testimonials">Testimonials</a>
            </li>
            <li class="nav-item"> <a class="nav-link" href="#about">About</a>
            </li>
            <li class="nav-item"> <a class="nav-link" href="#portfolio">Portfolio</a>
            </li>
            <li class="nav-item"> <a class="nav-link" href="#about">Design</a>
            </li>
            <li class="nav-item"> <a class="nav-link" href="#contact">Contact</a>
            </li>
        </ul>
    </div>
</nav>

CSS 的CSS

.navbar {
    display: none;
}

JS JS

$("body, html").on("scroll", function() {
    var features_top = $("#features-icon").position().top;
    var top_of_window = $(window).scrollTop();
    if (top_of_window >= features_top) {
        $(".navbar").show();
    } else {
        $(".navbar").hide();
    }
});

I believe it has something has to do with the JS part of my code the problem why my navbar isn't displaying. 我认为这与我的代码的JS部分有关,即我的导航栏不显示的问题。

Your navbar is not displaying because you never reach the point where the top_of_window >= features_top is valid. navbar未显示,因为您从未到达top_of_window >= features_top有效的位置。

I've updated the fiddle in the comment. 我在评论中更新了小提琴。 Check it now https://jsfiddle.net/8o7omnm7/33/ 立即检查https://jsfiddle.net/8o7omnm7/33/

Your navbar will be displayed only when the <div id="features-icon">Hola</div> will have -1px from top. 仅当<div id="features-icon">Hola</div>从顶部开始有-1px时,才会显示navbar As long as you don't have that it will not be displayed 只要您没有,它就不会显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM