Home > Blockchain >  Not able to change the color of navbar when I scroll
Not able to change the color of navbar when I scroll

Time:11-26

please help me iam new to css

I am not able to change my navbar color to red when I scroll

I think the problem is with the jquery iam using please correct me if iam wrong

I am using bootstrap 4

below is the Html I am using :

<!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">
    
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,300,400,700" rel="stylesheet">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    
    <!-- Custom CSS -->
    <link rel="stylesheet" href="app.css">

    <title>Museum of Candy</title>
  </head>
  
  <body>


    <nav id="MainNavbar" class="navbar navbar-expand-md navbar-dark py-0 fixed-top">
      <a href="#" class="navbar-brand">CANDY</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
          <a class="nav-link" href="#">HOME</a>
          <a class="nav-link" href="#">ABOUT</a>
          <a class="nav-link" href="#">TICKETS</a>
        </div>
      </div>
    </nav>

    <div id="text" class="container-fluid px-0">
      <div class="row align-items-center">
        <div class="col-lg-6 text-white text-center d-none d-lg-block mt-5">
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
        </div>
        <div class="col-lg-6">
          <img src="imgs/hand2.png" alt="hand image" class="img-fluid">
        </div>
      </div>
    </div>

    <section class="container-fluid px-0">
      <div class="row align-items-center content">
        <div class="col-md-6  order-md-1 order-2">
          <img src="imgs/milk.png" alt="" class="img-fluid">
        </div>
        <!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
        <div class="col-md-6 text-center order-md-2 order-1">
           <div class="row justify-content-center">
              <div class="col-10 col-lg-8 blurb mb-md-0 mb-5">
                <h2 class="">MUSEUM OF CANDY</h2>
                <img src="imgs/lolli_icon.png" alt="" class="d-none d-lg-inline">
                <p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
              </div>
            </div>
          </div>
      </div>



      <div class="row align-items-center content">
        <div class="col-md-6">
          <div class="row justify-content-center">
            <div class="col-10 col-lg-8 text-center blurb mb-md-0 mb-5">
              <h2 class="">MUSEUM OF CANDY</h2>
              <img src="imgs/lolli_icon.png" alt="" class="d-none d-lg-inline">
              <p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
            </div>
          </div>
        </div>
        <!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
        <div class="col-md-6 text-center">
          <img src="imgs/gumball.png" alt="" class="img-fluid">
        </div>
      </div>




      <div class="row align-items-center content">
        <div class="col-md-6  order-md-1 order-2">
          <img src="imgs/sprinkles.png" alt="" class="img-fluid">
        </div>
        <!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
        <div class="col-md-6 text-center order-md-2 order-1">
           <div class="row justify-content-center">
              <div class="col-10 col-lg-8 blurb mb-md-0 mb-5">
                <h2 class="">MUSEUM OF CANDY</h2>
                <img src="imgs/lolli_icon.png" alt="" class="d-none d-lg-inline">
                <p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
              </div>
            </div>
          </div>
      </div>

      
    </section>


      

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV 2 9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3 MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
   
    <script>
      $(function () {
          $(document).scroll(function () {
              var $nav = $("#mainNavbar");
              $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
          });
      });
  </script>
  
  </body>
</html>

I have used some css of my own

And my css is as follows:

body{
    background-color: #f5d9d5;
    font-family: "Nunito";
}

#MainNavbar{
    font-size: 1.5rem;
    font-weight: 100;
}
#MainNavbar .navbar-brand{
    color: #ea1c2c;
    font-size: 1.5rem;
}

#MainNavbar .nav-link{
    color: white;
}

#MainNavbar .nav-link:hover{
    color: #ea1c2c;
}

#text .slash{
    color: #ea1c2c;
}

h1{
    font-weight: 100;
    font-size: 4rem;
    
}

.blurb h2{
    color: #ea1c2c;
    font-weight: 100;
    font-size: 2.5rem;
}

.blurb p{
    font-weight: 100;
    font-size: 1.125rem;
    line-height: 2;
    color: #f498b8;
}

.content{
    margin-top: 100px;
    margin-bottom: 100px;
}

.navbar.scrolled {
    background-color: red
}



@media (max-width:1200px){
    h1{
        font-weight: 100;
        font-size: 3rem;
    }
    .blurb h2{
        font-size:2rem;
    }
}

click the link below to see my project folder structure

here is my project folder structure

CodePudding user response:

You will need JavaScript to do this, check out this pen.

// Logic
header {
    width: 100%;
  min-height: 3em;
  background-color: #eee;
}

html:not([data-scroll='0']) {
    
    body {
      padding-top: 3em;
    }
    
    header {
        position: fixed;
        top: 0;
        background-color: red;
        box-shadow: 0 0 .5em rgba(0, 0, 0, .5);
    }
    
}


// Cosmetics
header {
    box-sizing: border-box;
    padding: 1em;
    transition: background-color .25s;
}

main {
    padding:0 1em;
}

Js

> // The debounce function receives our function as a parameter const
> debounce = (fn) => {
> 
>   // This holds the requestAnimationFrame reference, so we can cancel
> it if we wish   let frame;
> 
>   // The debounce function returns a new function that can receive a
> variable number of arguments   return (...params) => {
>     
>     // If the frame variable has been defined, clear it now, and queue for next frame
>     if (frame) { 
>       cancelAnimationFrame(frame);
>     }
> 
>     // Queue our function call for the next frame
>     frame = requestAnimationFrame(() => {
>       
>       // Call our function and pass any params we received
>       fn(...params);
>     });
> 
>   }  };
> 
> 
> // Reads out the scroll position and stores it in the data attribute
> // so we can use it in our stylesheets const storeScroll = () => {  
> document.documentElement.dataset.scroll = window.scrollY; }
> 
> // Listen for new scroll events, here we debounce our `storeScroll`
> function document.addEventListener('scroll', debounce(storeScroll), {
> passive: true });
> 
> // Update scroll position for first time storeScroll();
  • Related