RA19
RA19

Reputation: 819

CSS nav background colour change only when scrolled to first section tag

Using bootstrap framework I am trying to change the navbar background colour from transparent to white. It is currently working when i scroll slightly using navbar-shrink class. However I want to change the navbar colour from transparent to white only once I get to the first section tag not straight away when the nav bar shrinks...

#mainNav.navbar-shrink {
    border-bottom: 1px solid rgba(33,37,41,.1);
    background-color: #fff;
}

.container {
    background-color: grey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
    <div class="container">
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link js-scroll-trigger selected" href="#about">About</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link js-scroll-trigger" href="#services">Services</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link js-scroll-trigger" href="#portfolio">Projects</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<section class="bg-primary first" id="about">
    <div class="container">
        <div class="row">
</section>

I want the background colour to change only when i scroll to the section tag (id = about) and not when the navbar shrinks

Upvotes: 0

Views: 577

Answers (2)

RA19
RA19

Reputation: 819

$(document).ready(function () {
    $(window).on("scroll", function () {
        if ($(window).scrollTop() >= 800) {
            $(".navbar").addClass("navbar-shrink ");
        } else {
            $(".navbar").removeClass("navbar-shrink ");
        }
    });
});

Upvotes: 0

Neil VanLandingham
Neil VanLandingham

Reputation: 1086

If you are using the ScrollSpy plugin to handle updating the active nav link, you can use the following solution. This just turns the navbar white when the #about section is scrolled to.

$('[data-spy="scroll"]').on('activate.bs.scrollspy', function (e) {
  var tabId = $(e.target).find('a').attr("href");
  if(tabId == "#about"){
    $("#mainNav").css("background-color","#fff")
  }
})

Upvotes: 2

Related Questions