Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 274

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T09:21:07+00:00 2024-11-25T09:21:07+00:00

How to change navbar style on scroll in React JS/Next JS

  • 62k

Today I am going to tell you one of the simplest ways to change the navbar styling on scroll.So first of all.Lets create a component Header.I am pasting the bootstrap simple code for Header just to keep things as simple as I can.To use the html css of bootstrap we have to first convert it in jsx which you can easily do by going to this website.

htmltojsx
Simply paste the code copied from bootstrap navbar and paste it on this website.It will return you with jsx code.
Header.jsx

 <nav className="navbar navbar-expand-lg navbar-light bg-light">         <a className="navbar-brand" href="#">Navbar</a>         <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">           <span className="navbar-toggler-icon" />         </button>         <div className="collapse navbar-collapse" id="navbarSupportedContent">           <ul className="navbar-nav mr-auto">             <li className="nav-item active">               <a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>             </li>             <li className="nav-item">               <a className="nav-link" href="#">Link</a>             </li>             <li className="nav-item dropdown">               <a className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">                 Dropdown               </a>               <div className="dropdown-menu" aria-labelledby="navbarDropdown">                 <a className="dropdown-item" href="#">Action</a>                 <a className="dropdown-item" href="#">Another action</a>                 <div className="dropdown-divider" />                 <a className="dropdown-item" href="#">Something else here</a>               </div>             </li>             <li className="nav-item">               <a className="nav-link disabled" href="#">Disabled</a>             </li>           </ul>           <form className="form-inline my-2 my-lg-0">             <input className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" />             <button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>           </form>         </div>       </nav> 
Enter fullscreen mode Exit fullscreen mode

and its css will be

Header.css

.navbar {     background-color: #a8dcef;     height: 105px;     width: 100%;     position: fixed;     z-index: 1000;     border-bottom: 1px solid rgb(192, 192, 192); }  
Enter fullscreen mode Exit fullscreen mode

Now we will use useEffect hook to

  • Read the scroll position and store it in the data attribute.
  • Listen for new scroll events.
  • Update scroll position.

So this is the code you have to paste it in useEffect hook in Header.jsx

useEffect(() => {         // 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();     }) 
Enter fullscreen mode Exit fullscreen mode

Now Last Step.For changing the styling we can use this css.In Header.css

html:not([data-scroll="0"]) .navbar {     position: fixed;     top: 0;     width: 100%;     /* background-color: #fff; */     opacity: 0.9;     box-shadow: 0 0 0.2em rgba(0, 0, 0, 0.5);     transition: 0.4s linear; } 
Enter fullscreen mode Exit fullscreen mode

Note:-

And thats it.Now you will see a change when you scroll down.

This format can be used to change any class on scroll the format is

html:not([data-scroll="0"]) .Here Class name {     /* Write the code you want to change */ } 
Enter fullscreen mode Exit fullscreen mode

Share it if you found it helpful!
Let me know in the comments if you find any difficulty.I will be there to help you.
Thanks.

javascriptnavbarreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.