React Navbar Change Background Color on Scroll - React JS Website Tutorial

Поділитися
Вставка
  • Опубліковано 15 вер 2024

КОМЕНТАРІ • 203

  • @khomohzie
    @khomohzie 3 роки тому +8

    Thank you so much for this. You have no idea the number of websites and tutorials I read for this. This is exactly what I want 👏

  • @MrDillonowns
    @MrDillonowns 3 роки тому +10

    This code is wrong. You are putting an event listener outside of a use effect hook that removes the event when the component unmounts. This means every time your page rerenders you are adding another event listener to your window. This means you are slowing down your code since the event callback is firing off many times!

    • @gabrielprrd
      @gabrielprrd 3 роки тому +2

      Yeah, it would be better to add the event listener inside the useEffect and then also add a clean-up function that removes it.

    • @bryanromero4135
      @bryanromero4135 3 роки тому

      how the useEffect hook could be used so that it doesn't cause said problem friend?

    • @bryanromero4135
      @bryanromero4135 3 роки тому

      @@gabrielprrd what would be the solution in the code? some example?

    • @bryanromero4135
      @bryanromero4135 3 роки тому +1

      asi seria la solucion a ese problema
      useEffect(() => {
      const fixedNavbar = () => {
      //console.log(window.scrollY);
      if (window.scrollY >= 739) {
      setNavbar(true);
      } else if (window.scrollY {
      window.removeEventListener("scroll", fixedNavbar);
      }
      }, []);

  • @oscardosjb
    @oscardosjb 3 роки тому +5

    I'm currently in the progress of learning React JS/Native. Thank you so much!

  • @mathiasriissorensen6994
    @mathiasriissorensen6994 3 роки тому +29

    For those of you who leverage NextJS, you have to use useEffect around the window.addEventListener :
    React.useEffect(() => {
    window.addEventListener("scroll", changeNavbar);
    }, []);

    • @LevTheDev
      @LevTheDev 2 роки тому +1

      God sent 🙏

    • @vivekbhatt3932
      @vivekbhatt3932 2 роки тому +1

      thanks

    • @karthikpuvvula
      @karthikpuvvula Рік тому +1

      thank you

    • @solomonlekan4113
      @solomonlekan4113 Рік тому +1

      You fixed my problem. Thanks

    • @metalyx1
      @metalyx1 Рік тому +2

      please, don't forget about a cleanup functions in such cases. Or you will have face a memory leak.
      Add this code right before the end of this useEffect:
      return () => {
      window.removeEventListener("scroll", changeNavbar);
      }

  • @uzairabdullah208
    @uzairabdullah208 3 роки тому +1

    And here was me fretting over it!!!
    thanks a million man!

  • @jorgeb139
    @jorgeb139 3 роки тому +1

    Greattttt thanks a lot, i'm learning React and i've been looking exactly for this, your form is so easy

  • @jorgegarnicablanco8136
    @jorgegarnicablanco8136 3 роки тому +3

    Let me share some code to improve memory usage:
    useEffect(() => {
    const changeHeader = () => {
    if (window.scrollY >= 80) {
    setActiveHeader(true);
    } else {
    setActiveHeader(false);
    }
    };
    window.addEventListener('scroll', changeHeader);
    return () => {
    window.removeEventListener('scroll', changeHeader);
    };
    }, []);
    Each time that Header mounts on the UI, a new function is associated with the Listener...

  • @kokuxz3837
    @kokuxz3837 3 роки тому +3

    I tried your solution and I found out that if I resize the screen that this is not going to work because the scrollY location on the screen changed so that if statement not working properly.

  • @youtube-video-watching-now
    @youtube-video-watching-now 7 місяців тому

    Thank you for your clean step by step explanation.
    I am applying it to my intern project.

  • @CreativeWorkersBest
    @CreativeWorkersBest 4 роки тому +2

    I liked your tutorials. I am sharing your videos my friends. We wait a lot of react js tutorials.

    • @briandesign
      @briandesign  4 роки тому

      Thanks! I have another react website tutorial I'm editing right now, so it'll be up in a few days

    • @CreativeWorkersBest
      @CreativeWorkersBest 4 роки тому

      @@briandesign Now I am learning react js. If I have questions realted to react js, could I ask?

    • @briandesign
      @briandesign  4 роки тому

      sure

  • @aleemjaved4420
    @aleemjaved4420 3 роки тому

    I'm currently in the progress of learning React JS Thank you so much!

  • @charlesbaker2851
    @charlesbaker2851 11 місяців тому +1

    Still super valid and super simple! Thank you!!!

  • @nicolasyracing
    @nicolasyracing 3 роки тому +2

    Thank you, very good and straight-to-the-point tutorial!!

  • @christinaharris9528
    @christinaharris9528 3 роки тому +3

    Thanks, I was trying to figure this out. I'm using styled-components as well as react-strap and it took me a bit to figure out how to do this without classes. Just in case anybody wants to know I didn't put an active class in my styled component. I did use the navbar useState code, the changeBackground function, and the addEventListener. In my component under the navigation tag I used jsx style to insert the ternary it looked something like this :
    I'm sure there's also a way of doing this through styled-components with props but I'm still learning. If somebody figures it out let me know!

    • @favourp
      @favourp 2 роки тому +3

      For styled components, this is how you add the active class
      const Nav = styled.nav`
      height: 60px;
      display: flex;
      justify-content: space-between;
      padding: 1rem 2rem;
      z-index: 100;
      position: fixed;
      width: 100%;
      background: transparent;
      &.active {
      background: #cd853f;
      }
      `;
      This is how you implement it

    • @ekweisking2023
      @ekweisking2023 2 роки тому

      Thanks a lot, styled components is nice but it kinda makes everything feel different.

    • @ekweisking2023
      @ekweisking2023 2 роки тому

      @@favourp i think i like yours better

    • @pedrovictorsaraiva8705
      @pedrovictorsaraiva8705 2 роки тому

      @@ekweisking2023 Did it work?

  • @kurenpokaramu5230
    @kurenpokaramu5230 2 роки тому +2

    Why did you use event listener outside the useEffect? I think it's an anti-pattern for react though.

  • @capitanm2166
    @capitanm2166 3 роки тому +2

    Anyway to add a smooth transition like in css transition: .1s;?

  • @akbarmaulana517
    @akbarmaulana517 3 роки тому

    Thank you so much for creating this. It helps me a lot cause I want to know hot to change navbar's background color when I scroll the web.

  • @dheerajnaik6467
    @dheerajnaik6467 4 роки тому +1

    I am ur big fan. Following all your videos

  • @NargaKuruga
    @NargaKuruga 3 роки тому

    Love your channel man ! Great work, thanks for the efforts !

  • @Paul-wz1xw
    @Paul-wz1xw 4 роки тому +7

    Really helpful content man! Have you tried this with throttling so that the scroll event isn't firing every time someone is scrolling on the page?

    • @briandesign
      @briandesign  4 роки тому

      I haven't! I'll have to look into that

  • @marktwain3083
    @marktwain3083 2 роки тому

    Awesome tutorial, thanks Brian!

  • @saurabhjoshi5021
    @saurabhjoshi5021 3 роки тому

    this is what i am looking for
    thankyou very much

  • @gabrielfono844
    @gabrielfono844 2 роки тому

    I am using this logic to solve other problem on my web application

  • @joakov3
    @joakov3 4 роки тому

    I've been looking for this thank you man

  • @gijoe3211
    @gijoe3211 3 роки тому

    Awesome. Works with react-bootstrap too. Many thanks

  • @osman1110
    @osman1110 Рік тому

    When you position your scroll bar at exactly the 80px y, the animation keeps triggering. is there a way to stop this?

  • @adeolaa.366
    @adeolaa.366 3 роки тому

    subscribed!!! thank you Brian I was looking so hard for exactly this

    • @briandesign
      @briandesign  3 роки тому

      Glad I could help!

    • @adeolaa.366
      @adeolaa.366 3 роки тому

      @@briandesign is there any source code though im following the video but it would be nice to see sc?

  • @야채튀김-e1t
    @야채튀김-e1t 2 роки тому

    thank you very much!! It helped me a lot

  • @praveen_javali
    @praveen_javali 4 роки тому

    Thank you very much, I was looking for this.

  • @keshavsethi1610
    @keshavsethi1610 3 роки тому

    really cool man thanks a lot one. Really well done.

  • @kamiladewale5426
    @kamiladewale5426 4 роки тому

    You always nail it man. Kudos

  • @atirpok3
    @atirpok3 2 роки тому

    Thanks man. Super helpful!

  • @unmoybiswas5190
    @unmoybiswas5190 3 роки тому

    thanks a lot for such a beautiful tutorial

  • @Deddy676
    @Deddy676 3 роки тому

    Dude that so fancy, thanks !!

  • @DavidOSinger
    @DavidOSinger 3 роки тому

    any idea why instead of transparent my navbar keeps showing white ?

  • @Phyx1u5
    @Phyx1u5 3 роки тому

    awesome man this helped me heaps thanks

  • @BleedingDryTheHeart
    @BleedingDryTheHeart 4 роки тому

    That was helpful my friend , thank you :)

  • @EloxFire
    @EloxFire 3 роки тому

    Thanks, helpful and very clear explanations.

  • @SachinKulshreshtha6185
    @SachinKulshreshtha6185 3 роки тому

    Thank you very much for this video.

  • @bhagyeshpatel6472
    @bhagyeshpatel6472 2 роки тому

    Thank you so much for helpful content

  • @alidev6882
    @alidev6882 3 роки тому

    Hey you solved my problem thank u so much, and subscribed.

  • @chalermkhwannasritha7431
    @chalermkhwannasritha7431 4 роки тому

    Thank you so much! Subscribed!

  • @shivanshpratap3624
    @shivanshpratap3624 3 роки тому

    Thank You, It was Helpful.

  • @ani9415
    @ani9415 2 роки тому

    how will we apply this condition under styled components?

  • @SeeRjED
    @SeeRjED 3 роки тому

    What if I already have a conditional check on my navbar class? Is there a way to add an additional check?

  • @AangPer
    @AangPer 3 роки тому

    But how can I make the navbar change color in each section?, for example in the Hero it is transparent in the first section blue and in the second red and so on, I made that effect by creating a reference for each of the sections that I wanted and using getBoundingClientRect I made an if and else statement that when the top is less than and equal to 0 and the bottom is greater than and equal to 0 it changes of the color that I want but i think ist not a good aproach or are to many line for something that maybe should more easy

  • @a.j9157
    @a.j9157 2 роки тому

    Thanks man!

  • @ricardosolis9800
    @ricardosolis9800 2 роки тому

    thanks you can also put the addEventListener in a useEffect

  • @arnobchakma3080
    @arnobchakma3080 2 роки тому

    Thank you so much you are amazing...

  • @itspawanpoudel
    @itspawanpoudel 2 роки тому

    Thanks dude :)

  • @dalimkumardas3760
    @dalimkumardas3760 2 роки тому

    Thank you very much budddy

  • @jamesgabbitus
    @jamesgabbitus 2 роки тому

    thanks brah, very helpful

  • @chideraike
    @chideraike 3 роки тому

    Bro Thanks so much for this video!!

  • @radomirmijovic2605
    @radomirmijovic2605 3 роки тому

    Great video, thanks man

  • @ibadshaikh2215
    @ibadshaikh2215 4 роки тому

    Amazing as always buddy!

  • @FaeUtero
    @FaeUtero 3 роки тому

    Amazing! Thank you \m/

  • @anantgupta1216
    @anantgupta1216 4 роки тому

    bro u r a real GOD KEEP MAKING VIDEOS BROOO!!!

  • @bonnie6614
    @bonnie6614 4 роки тому

    Great tutorial! Subscribed!

  • @MisouSup
    @MisouSup 3 роки тому

    Thank you. Very simple stuff, but we wonder if we are doing it right XD

    • @briandesign
      @briandesign  3 роки тому +1

      Thanks, and yeah I got this straight from the docs

    • @MisouSup
      @MisouSup 3 роки тому

      @@briandesign oops. I meant that we always wonder how these components are made, but it turns out it's this simple

  • @ThienNguyen-ui8jl
    @ThienNguyen-ui8jl 3 роки тому

    Bro, is there any problem if this approach cause re-renders on every scrollY ? I'm a newbie so I don't which re-render cases are accepted. Thank you bro!

  • @hieuminh9402
    @hieuminh9402 3 роки тому

    this is what I'm looking for

  • @gabrielfono844
    @gabrielfono844 2 роки тому

    where did you get that amazing background
    is it svg
    please share resource if you can
    thanks

  • @mikeatilano4954
    @mikeatilano4954 3 роки тому

    how'd you get the navbar on top of the image/video of the hero section?

  • @NBA6Fan
    @NBA6Fan 3 роки тому +1

    You should've mentioned that this must go in useEffect otherwise it is too much of event. Other than that, thank you for tutorial.

    • @DebjitMajumdar
      @DebjitMajumdar 2 роки тому

      UseEffect and Checking the mounted state, else you get an error for Window is not defined. Especially for nextjs where the server renders the page first.

  • @daudahmed2343
    @daudahmed2343 2 роки тому

    Thanks for this

  • @mentalizing
    @mentalizing 9 місяців тому

    thanks from brazil

  • @romualdastamulaitis7182
    @romualdastamulaitis7182 Рік тому

    When using 'Back' button, browser remembers scrolled position, but navigation has initial transparent color. When scroll - it changes color as it should. Does anyone know how to display scrolled color with 'Back' button?

  • @AryanKumar-cc7ku
    @AryanKumar-cc7ku 3 роки тому

    Full video of this project from starting?? Can anyone share the link

  • @JeanSantos97
    @JeanSantos97 3 роки тому

    How to change the style (background-color) of a navbar, depending on the page I am on in React?, for example:
    I'm on the index page, the navbar at the beginning has a black color, when scrolling it changes to red,
    Then I go to the services page, the navbar at the beginning has a blue color, when scrolling it changes to yellow, and if I go back to the index I have the colors already defined for the index.
    Thanks

  • @calmyourmind5617
    @calmyourmind5617 Рік тому

    Thank you very much

  • @gabrielfillon11
    @gabrielfillon11 2 роки тому

    How do I use this on styled components? I want to be able to pass to my styled components but don't know the proper syntax

  • @EveryThingOnNow
    @EveryThingOnNow 3 роки тому

    Its not working at me, it shows white space in navbar

  • @bintangyogapamungkas1225
    @bintangyogapamungkas1225 3 роки тому

    Thanks for sharing 🙌

  • @NayemIslam-ji3dq
    @NayemIslam-ji3dq 3 місяці тому

    Thanks a lot dear

  • @Achiesamablog
    @Achiesamablog 2 роки тому

    idk, I remember some guy telling me I always have to clean the event (or something) whenever i use event listener in react. Any one got some clue to it?

  • @krcpr007
    @krcpr007 2 роки тому

    how i can do this in nextJS also this method is not working in nextJs.

  • @Screamer-jy5db
    @Screamer-jy5db 2 роки тому +1

    good job

  • @aryajawarkar3861
    @aryajawarkar3861 Рік тому

    How did you add the rotating planet in background can please share the link or help me
    I want to do it too

  • @julieno.2053
    @julieno.2053 3 роки тому

    Thank you very much !

  • @svsamvalvi
    @svsamvalvi 3 роки тому

    Good video! Thanks!

  • @musadanjuma6603
    @musadanjuma6603 4 роки тому +1

    I'M GLAD I SAW THIS. THANK YOU SO MUCH!!!

  • @easytutorialdarija
    @easytutorialdarija 4 роки тому

    thanks bro , you are best

  • @wellyesbutno5639
    @wellyesbutno5639 2 роки тому

    isnt this a bit expensive as the function will be run on every single scroll

  • @naumanshigri
    @naumanshigri 2 роки тому

    thank. you so much

  • @ferdianfh
    @ferdianfh 2 роки тому

    hey brian thank you for this great tutorial! do you still answer? i have a question, what if i want to change the color twice in different scrollY height?

  • @stephensander3061
    @stephensander3061 3 роки тому

    Thanks so much!!!

  • @h.m.zakariasumon1644
    @h.m.zakariasumon1644 2 роки тому

    Thank you boss

  • @everson_vinicius
    @everson_vinicius Рік тому

    excelent!

  • @yacouvbanou6886
    @yacouvbanou6886 3 роки тому

    oh men you are good, thanks

  • @jhonrvlogger
    @jhonrvlogger 3 роки тому

    Thank you so much!!!! hey i have a question do you know how can i do a dropdown in the menu using NavLink ?????

    • @briandesign
      @briandesign  3 роки тому

      I made a dropdown navbar vid, so you can check that out and then refactor it using a NavLink

  • @exeibier811
    @exeibier811 3 роки тому

    Hi! excellent video but im running into a problem, i get the error window is not defined. if anyone can help me would be great. thanks!

  • @namozostonayev9187
    @namozostonayev9187 4 роки тому

    Great. thank you so much

  • @bellohargbola4090
    @bellohargbola4090 3 роки тому

    make sure you remove the event listener once the component is unmounted

  • @kfarooqabdulla8967
    @kfarooqabdulla8967 4 місяці тому

    very good

  • @hackzyoboy2459
    @hackzyoboy2459 3 роки тому

    Man, how would you use Intersection Observer in this???

  • @SzTz100
    @SzTz100 2 роки тому

    Great video, which VSCode theme are you using ?

  • @Seven77tw
    @Seven77tw 3 роки тому

    you're the best

  • @naiadbaksh3996
    @naiadbaksh3996 3 роки тому +3

    Hey man, love the tips! I came here from the last vid, one thing is when I make the navbar class background: transparent; it does make it transparent but it's leaves a white box up top where the navbar used to be and once you scroll you can see the navbar information like logo and links with a transparent background. How do you get rid of that white box across the screen. I just want it to look like yours at the beginning of the video.

    • @naiadbaksh3996
      @naiadbaksh3996 3 роки тому

      Can anyone help me with this issue? I've been trying for some time, and I'm concerned I'll have to restructure the website totally.

    • @thiagocabralcorreia
      @thiagocabralcorreia 3 роки тому +1

      @@naiadbaksh3996 Man, I took a long time trying to solve this issue too. So I had the idea to access the cover style file, where the video is, and in the main container I put margin-top: -80px (since the height of the navbar is 80px, and so the video went up on the screen, to behind the navbar).

    • @thiagocabralcorreia
      @thiagocabralcorreia 3 роки тому +1

      It will be necessary to make other adjustments, such as adding a background-color to the navbar when resizing it, in the media queries, and adjusting the alignment of the central text of the cover, centering it.

  • @ericmartinez7889
    @ericmartinez7889 3 роки тому

    Hi Brian excellent tutorial. I'm having a problem with the navbar when the hamburger menu is displayed. It gets transparent and the menu gets black. How can I fix it?

    • @briandesign
      @briandesign  3 роки тому +1

      you can try adding a negative margin: -80px; to bring the hero section up cause it sounds like the navbar is stacking on top of the hero section, so you could bring that section up. Or if you wanted to keep the color of the nav the same as the mobile menu, then you can create a function that changes the color of the nav to black if the window width is < 768px for example

  • @오윤석-j2q
    @오윤석-j2q 3 роки тому +1

    Hello. I am a subscriber who likes you.
    Thank you for always learning everything about React.
    There was a problem this time. Also, downloading your GitHub didn't solve the problem. The problem is below. It is different from the video ".navbar{ background: transparent;}". The navbar does not become transparent and turns white.

    • @오윤석-j2q
      @오윤석-j2q 3 роки тому

      Thank you all the time.

    • @오윤석-j2q
      @오윤석-j2q 3 роки тому

      .navbar {background : transparent;} nok working

    • @briandesign
      @briandesign  3 роки тому +1

      Add margin-top: -80px cause it’s there it’s just above the hero section

    • @오윤석-j2q
      @오윤석-j2q 3 роки тому +1

      @@briandesign I solved the problem. Thank you so much. I respect you.