Home > Mobile >  Why won't my navbar change color with js scroll function?
Why won't my navbar change color with js scroll function?

Time:10-26

My navbar doesn't seem to be changing color when scrolled. There's a bit of js I'm trying to implement (at the bottom of the following code block), but it doesn't seem to render and I have no idea why. I've made sure the bootstrap cdn is up-to-date. I've made sure my class references are accurate. Nothing seems to work.

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="preconnect" href="https://fonts.googleapis.com"> 
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Nunito Sans:ital,wght@0,600;0,800;1,300&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <link rel="stylesheet" href="app.css">

    <title> Museum of Candy </title>
</head>
 
<body>
    <nav id="#mainNavbar" >
        <a href="#" >CANDY</a>
        <button  data-toggle="collapse" data-target="#navLinks" aria-label="Toggle navigation">
            <span > </span>
        </button>
        <div  id="navLinks">
            <ul >
                <li >
                    <a href="" >HOME</a>
                </li>
                <li >
                    <a href="" >ABOUT</a>
                </li>
                <li >
                    <a href="" >TICKETS</a>
                </li>
            </ul>
        </div>
    </nav>
    
    <section >
        <div >
            <div >
                <div id="headingGroup" >
                    <h1 >MUSEUM<span>/</span>OF<span>/</span>CANDY</h2>
                    <h1 >MUSEUM<span>/</span>OF<span>/</span>CANDY</h2>    
                    <h1 >MUSEUM<span>/</span>OF<span>/</span>CANDY</h2>    
                    <h1 >MUSEUM<span>/</span>OF<span>/</span>CANDY</h2>
                    <h1 >MUSEUM<span>/</span>OF<span>/</span>CANDY</h2>    
                    <h1 >MUSEUM<span>/</span>OF<span>/</span>CANDY</h2>   
                </div>
            </div>
            <div >
                <img  src="hand2.png" alt="">
            </div>
        </div>
    </section>

    <section >
        <div >
            <div >
                <img  src="milk.png" alt="">
            </div>
            <div >
                <div >
                    <div >
                        <h2>MUSEUM OF CANDY</h2>
                        <img src="lolli_icon.png" alt="" >
                        <p >Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque, iste molestiae...</p>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <section >
        <div >
            <div >
                <div >
                    <div >
                        <h2>MUSEUM OF CANDY</h2>
                        <img src="lolli_icon.png" alt="" >
                        <p >Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque, iste molestiae... </p>
                    </div>
                </div>
            </div>
            <div >
                <img  src="gumball.png" alt="">
            </div>
        </div>
    </section>
    <section >
        <div >
            <div >
                <img  src="sprinkles.png" alt="">
            </div>
            <div >
                <div >
                    <div >
                        <h2>MUSEUM OF CANDY</h2>
                        <img src="lolli_icon.png" alt="" >
                        <p >Lorem ipsum dolor sit amet consectetur adipisicing elit. Neque, ist...</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X 965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH 8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-B0UglyR jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
    
<!-- This is the JS section that's not working: -->

    <script>
        $(function () {
            $(document).scroll(function () {
                var $nav = $("#mainNavbar");
                $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
            });
        });
    </script>
</body>

</html>

And, for what it's worth, here is my css:

.navbar.scrolled {
    background: rgb(222,192,222);
    transition: background 500ms;
}

I want the navbar background color to change when I scroll, but that doesn't seem to be happening.

CodePudding user response:

Notice, you have a little mistake, you declared id and wrote #mainNavbar, just delete that # id="mainNavbar"

CodePudding user response:

Remove hash from nav id. Check the image here. https://prnt.sc/V-NkibDZCUYi

  • Related