jqueryhtmltwitter-bootstrapbootstrap-4scrollspy

Bootstrap 4 scrollspy not working to change active navbar items


I am using Bootstrap 4.0 to create a simple landing page and using ScrollSpy in this page but I am not able to make it work, it does not changing the active menu items.

index.html -

<body>

<!-- Navbar -->
<nav id="main-nav" class="navbar navbar-expand-lg bg-light bg-transparent fixed-top">
    <div class="container">

        <a class="navbar-brand" href="#"><!-- some logo --></a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse justify-content-end" id="navbarSupportedContent">
            <ul class="navbar-nav">
                <li class="nav-item"><a class="nav-link" href="#header">Home</a></li>
                <li class="nav-item"><a class="nav-link" href="#book">About Book</a></li>
                <li class="nav-item"><a class="nav-link" href="#author">About Author</a></li>
                <li class="nav-item"><a class="nav-link" href="#inspire">Inspire</a></li>
            </ul>
        </div>

    </div>
</nav>

<!-- Header -->
<header id="header" class="header">
    <!-- some content -->
</header>

<!-- About book -->
<section id="book" class="book py-5">
    <!-- some content -->
</section>

<!-- About author -->
<section id="author" class="author py-5">
    <!-- some content -->
</section>

<!-- Inspire -->
<section id="inspire" class="author py-5">
    <!-- some content -->
</section>

<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.min.js"></script>
</body>

I have body{postsition: relative} in my style.css file.

main.js -

$(document).ready(function () {

    // Scroll spy
    $('body').scrollspy({
        target: "#main-nav",
        offset: 70
    });

    // Navbar fade
    changeNavbar();

    $(window).scroll(function () {
        changeNavbar();
    });

    function changeNavbar() {
        var navbar = $("#main-nav");
        if ($(this).scrollTop() >= 100) {
            navbar.addClass("bg-light").removeClass("bg-transparent");
        } else if ($(this).scrollTop() < 100) {
            navbar.removeClass("bg-light").addClass("bg-transparent");
        }
    }
});

I searched a lot but didn't get any solution, already wasted few hours, any help will be appreciated.

Thanks in advance.


Solution

  • The ScrollSpy component is applying the active class to the nav-item, but you can't see it because you don't have navbar-light class in the navbar.

    The Bootstrap 4 CSS selector that applies the style (darker text color) is this:

    .navbar-light .navbar-nav .nav-link.active {
      color: rgba(0, 0, 0, 0.9);
    }
    

    So, just add the navbar-light class like this:

    <nav id="main-nav" class="navbar navbar-expand-lg navbar-light bg-light bg-transparent fixed-top">
    </nav>
    

    Demo: https://www.codeply.com/go/ic6Md3e4y1