I am trying to use bootstrap 5 to make a scrolling transparent navbar, however I am using jQuery and I need to convert it to react or simple javascript.
Here’s my HTML:
<!-- Navbar--> <header class="header"> <nav class="navbar navbar-expand-lg fixed-top py-3"> <div class="container"><a href="#" class="navbar-brand text-uppercase font-weight-bold">Transparent Nav</a> <button type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler navbar-toggler-right"><i class="fa fa-bars"></i></button> <div id="navbarSupportedContent" class="collapse navbar-collapse"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"><a href="#" class="nav-link text-uppercase font-weight-bold">Home <span class="sr-only">(current)</span></a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">About</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Gallery</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Portfolio</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Contact</a></li> </ul> </div> </div> </nav> </header> <!-- For demo purpose --> <div class="container"> <div class="pt-5 text-white"> <header class="py-5 mt-5"> <h1 class="display-4">Transparent Navbar</h1> <p class="lead mb-0">Using Bootstrap 4 and Javascript, create a transparent navbar which changes its style on scroll.</p> <p class="lead mb-0">Snippet by <a href="https://bootstrapious.com" class="text-white"> <u>Bootstrapious</u></a> </p> </header> <div class="py-5"> <p class="lead">Lorem ipsum dolor sit amet, <strong class="font-weight-bold">consectetur adipisicing </strong>elit. Explicabo consectetur odio voluptatum facere animi temporibus, distinctio tempore enim corporis quam <strong class="font-weight-bold">recusandae </strong>placeat! Voluptatum voluptate, ex modi illum quas nam distinctio.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div class="py-5"> <p class="lead">Lorem ipsum dolor sit amet, <strong class="font-weight-bold">consectetur adipisicing </strong>elit. Explicabo consectetur odio voluptatum facere animi temporibus, distinctio tempore enim corporis quam <strong class="font-weight-bold">recusandae </strong>placeat! Voluptatum voluptate, ex modi illum quas nam distinctio.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> </div> </div>
And here’s my jQuery that I need to convert:
$(function () { $(window).on('scroll', function () { if ( $(window).scrollTop() > 10 ) { $('.navbar').addClass('active'); } else { $('.navbar').removeClass('active'); } }); });
You can see the JSFIDDLE here: https://jsfiddle.net/bootstrapious/ravpqxok/
Any idea how can I transform this into react or javascript version?
Advertisement
Answer
use window.addEventListener
to catch the scroll event and classList.add
/ classList.remove
to add remove the class. this.scrollY
will give you he same result as
$(window).scrollTop()
window.addEventListener("scroll", function(e) { const navBar = document.querySelector(".navbar"); if (this.scrollY > 10) { navBar.classList.add("active"); } else { navBar.classList.remove("active"); } console.log(navBar.classList); })
/* * * ========================================== * CUSTOM UTIL CLASSES * ========================================== * */ .navbar { transition: all 0.4s; } .navbar .nav-link { color: #fff; } .navbar .nav-link:hover, .navbar .nav-link:focus { color: #fff; text-decoration: none; } .navbar .navbar-brand { color: #fff; } /* Change navbar styling on scroll */ .navbar.active { background: #fff; box-shadow: 1px 2px 10px rgba(0, 0, 0, 0.1); } .navbar.active .nav-link { color: #555; } .navbar.active .nav-link:hover, .navbar.active .nav-link:focus { color: #555; text-decoration: none; } .navbar.active .navbar-brand { color: #555; } /* Change navbar styling on small viewports */ @media (max-width: 991.98px) { .navbar { background: #fff; } .navbar .navbar-brand, .navbar .nav-link { color: #555; } } /* * * ========================================== * FOR DEMO PURPOSES * ========================================== * */ .text-small { font-size: 0.9rem !important; } body { min-height: 110vh; background-color: #4ca1af; background-image: linear-gradient(135deg, #4ca1af 0%, #c4e0e5 100%); }
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Navbar--> <header class="header"> <nav class="navbar navbar-expand-lg fixed-top py-3"> <div class="container"><a href="#" class="navbar-brand text-uppercase font-weight-bold">Transparent Nav</a> <button type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler navbar-toggler-right"><i class="fa fa-bars"></i></button> <div id="navbarSupportedContent" class="collapse navbar-collapse"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"><a href="#" class="nav-link text-uppercase font-weight-bold">Home <span class="sr-only">(current)</span></a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">About</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Gallery</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Portfolio</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Contact</a></li> </ul> </div> </div> </nav> </header> <!-- For demo purpose --> <div class="container"> <div class="pt-5 text-white"> <header class="py-5 mt-5"> <h1 class="display-4">Transparent Navbar</h1> <p class="lead mb-0">Using Bootstrap 4 and Javascript, create a transparent navbar which changes its style on scroll.</p> <p class="lead mb-0">Snippet by <a href="https://bootstrapious.com" class="text-white"> <u>Bootstrapious</u></a> </p> </header> <div class="py-5"> <p class="lead">Lorem ipsum dolor sit amet, <strong class="font-weight-bold">consectetur adipisicing </strong>elit. Explicabo consectetur odio voluptatum facere animi temporibus, distinctio tempore enim corporis quam <strong class="font-weight-bold">recusandae </strong>placeat! Voluptatum voluptate, ex modi illum quas nam distinctio.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <div class="py-5"> <p class="lead">Lorem ipsum dolor sit amet, <strong class="font-weight-bold">consectetur adipisicing </strong>elit. Explicabo consectetur odio voluptatum facere animi temporibus, distinctio tempore enim corporis quam <strong class="font-weight-bold">recusandae </strong>placeat! Voluptatum voluptate, ex modi illum quas nam distinctio.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> </div> </div>