Click here to Skip to main content
16,018,318 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to have my navigation bar change from transparent background to a black background when scrolling.

This is my html:
HTML
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>Cuppela</title>
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/bootstrap.min.js" type="text/javascript"></script>
    <script src="js/popper.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="css/test.css" type="text/css">
</head>
<body>
<div id="nav-menu">
    <nav id="main-nav" class="navbar navbar-expand-md fixed-top navbar-dark bg-transparent">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
            
          </button>
          <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="nav navbar-nav mx-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Shop</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">About</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                </li>
            </ul>
        </div>
    </nav>
</div>

<script>
    $(document).ready(function(){
        $(window).on("scroll",function(){
    
            if($(document).scrollTop() > 50)
            {
                $("#main-nav").css('background', 'black !important;');
            }
            else
            {
                $("#main-nav").css('background','transparent !important;');
            }
        })
    });
</script>

<div class="container-fluid" id="top-main"> 

    <div id="top_text">
        <h1>Cuppela Made With Sugar</h1>
        <p style="font-size: x-large;">Delicious Homemade Cakes</p>
    </div>
    
</div>

<div id="best-sellers "class="container">
    <h2>Best Sellers</h2>
    <hr>
    <div id="best-seller-cakes" class="row">
        <div class="col-xs-12 col-sm-6 col-lg-3">
            <img src="images/chocolate_cake.jpg">
            <h4>Good<br>Cakes</h4>
            <p>Delicious Cake with Awesome Goodness</p>
        </div>
        <div class="col-xs-12 col-sm-6 col-lg-3">
            <img src="images/red_velvet_cake.jpeg">
            <h4>Awesome<br>Cakes</h4>
            <p>Delicious Cake with Awesome Goodness</p>
        </div>
        <div class="col-xs-12 col-sm-6 col-lg-3">
            <img src="images/strawberry_cake.jpg">
            <h4>Great<br>Cakes</h4>
            <p>Delicious Cake with Awesome Goodness</p>
        </div>
        <div class="col-xs-12 col-sm-6 col-lg-3">
            <img src="images/cake_seven.jpg">
            <h4>Delicious<br>Cakes</h4>
            <p>Delicious Cake with Awesome Goodness</p>
        </div>
    </div>
</div>

<footer>
    <div class="container-fluid" id="footer-content">
    <h3>Test</h3>
    </div>
</footer>

</body>
</html>


This is my css:
CSS
body{
    margin: 0px;
}

li a:hover {
    background: #ff9900;
    border-radius: 10px;
}

li a{
    color: white !important;
}

.nav-link{
    padding: 0 0 .2rem
}

#top-main{
    background-image: url(../images/cake_six_two.jpg);
    height: 100vh;
}

#nav-menu{
    font-size: medium;
    position: relative;
}

#top_text{
    color: white;
    text-align: center;
    position: relative;
    top: 50%;
    left: 50%;
    /* bring your own prefixes */
    transform: translate(-50%, -50%);
}

h1{
    font-size: 500%;
    text-align: center;
}


h2{
    text-align: center;
}

#best-sellers{
    text-align: center;
}

#best-seller-cakes{
    text-align: center;
}

h4
{
    font-size: x-large;
    font-weight: bold;
    font-family: Georgia, 'Times New Roman', Times, serif;
}

#footer-content
{
    text-align: center;
}


What I have tried:

I have tried solutions from javascript - How to Change Navigation Bar Background with Scroll? - Stack Overflow[^] and javascript - Changing nav-bar color after scrolling? - Stack Overflow[^] but it does not work for me.
Posted
Updated 5-Aug-21 0:09am
v2
Comments
Richard Deeming 5-Aug-21 5:48am    
I have removed the link to your website, since it made your question look a bit like spam. You've pasted the relevant code into your question, so there's no need for the link.

1 solution

I'd be inclined to toggle a class on the body element, and use CSS to control the colours. For example:
JavaScript
$(function() {
    $(window).on("scroll", function() {
        $("body").toggleClass("scrolled", $(document).scrollTop() > 50);
    })
});
CSS
#main-nav{
    background: transparent;
    transition: background 0.3s;
}
.scrolled #main-nav {
    background: black;
}
Demo[^]

Edit: If you can drop support for Internet Explorer, you can use the Intersection Observer API to make this slightly more efficient:
JavaScript
$(function() {
    const scrollPoint = 50;

    let targetPixel = document.createElement("div");

    targetPixel.style.cssText = `
  position: absolute;
  top: ${scrollPoint}px;
  left: 0;
  width: 1px;
  height: 1px;
`;

    targetPixel = document.body.appendChild(targetPixel);

    const observer = new IntersectionObserver(entries => {
        const scrolled = entries[0].boundingClientRect.y < 0;
        if (scrolled) {
            document.body.classList.add("scrolled");
        } else {
            document.body.classList.remove("scrolled");
        }
    });

    observer.observe(targetPixel);
});
Demo[^]
Intersection Observer API - Web APIs | MDN[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900