How to change your navigation style on scroll

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ก.ค. 2024
  • This video explores using the Intersection Observer API to watch for an element leaving the page and then changing the style of a fixed navigation bar.
    GitHub repo: github.com/kevin-powell/navba...
    (includes start and finished versions)
    I set up some custom properties ahead of time to make the change really super simple, and then with the use of a little JavaScript to set up our observer, we can add (or remove) a class from our navigation when it reaches the right point on the screen.
    #JavaScript #IntersectionObserver #CSS
    ---
    Come and hangout, ask questions, suggest ideas, and meet some awesome people over in The Community: / discord
    I have a newsletter! www.kevinpowell.co/newsletter
    New to Sass, or want to step up your game with it? I've got a course just for you: www.kevinpowell.co/learn-sass
    ---
    My Code Editor: VS Code - code.visualstudio.com/
    How my browser refreshes when I save: • How to automatically r...
    ---
    Support me on Patreon: / kevinpowell
    I'm on some other places on the internet too!
    If you'd like a behind the scenes and previews of what's coming up on my TH-cam channel, make sure to follow me on Instagram and Twitter.
    Instagram: / kevinpowell.co
    Twitter: / kevinjpowell
    Codepen: codepen.io/kevinpowell/
    Github: github.com/kevin-powell

ความคิดเห็น • 199

  • @user-nc7tk3ee3q
    @user-nc7tk3ee3q 5 ปีที่แล้ว +103

    Please don't stop this series, it is amazing and it really helps alot !

  • @pieter-venter
    @pieter-venter 5 ปีที่แล้ว +6

    Your videos never seem to amaze me. TH-cam is full of videos on the basics of CSS but your videos focus on the more advanced things that people might not even have heard of.
    Thank you for that!

  • @cintron3d
    @cintron3d 5 ปีที่แล้ว +6

    Thank you so much! I thought I knew CSS and JS but then I started seeing these sites with fancy transitions and didn't know how they were achieving that look. This video is so helpful thank you!

  • @nuqtamedia1294
    @nuqtamedia1294 2 ปีที่แล้ว

    I just spent 3 hours trying to wrap my head around this with no results. Then I found your video and it all makes sense now! Thanks a ton!!!

  • @muhammadraza2940
    @muhammadraza2940 2 ปีที่แล้ว +1

    Fantastic video! Really clean way to manage scroll events. Definitely my go to method now. Thanks a ton for this!

  • @ramadanmoheyeldeen6252
    @ramadanmoheyeldeen6252 4 ปีที่แล้ว +20

    You're the best instructor I have ever see, you deserve more than the best and I have learned a lot form you.

  • @cervece41
    @cervece41 4 ปีที่แล้ว +1

    You're a lifesaver, outstanding channel, I can't stop watching and liking your videos!!

  • @SuperSaiyanScandinavian
    @SuperSaiyanScandinavian 4 ปีที่แล้ว +1

    I dunno if you read the comments much, or if there are just too many comments to sift through, but i just wanna say your channel is absolutely fantastic. It's specifically your channel and DevEd that I come to all the time to learn new css tricks. im still relatively new to web development and am finishing up my first fullstack site. but the thing that has lacked in all my education is styling. you guys have helped me to make my site and other demo stuff I've done recently look so much better than my earlier stuff. honestly if I land a job because of my portfolio, you guys are to thank. As a matter of fact, once I get my portfolio site going, i'm going to credit you guys in my about me section.

    • @KevinPowell
      @KevinPowell  4 ปีที่แล้ว

      I do my best to read them all :)
      Glad that you enjoy my content, and thanks for taking the time to let me know :D

  • @MARVTOBI-wz8qk
    @MARVTOBI-wz8qk 11 หลายเดือนก่อน

    Been battling on this for days now and just came across your channel... It's really helpful, thank you.

  • @blonduose
    @blonduose 4 ปีที่แล้ว +7

    Lovely, thanks, finally finished my product landing page with a few JS tweaks so it looks nicer :)

  • @tasteoftheair
    @tasteoftheair 3 ปีที่แล้ว

    Thank you for a great video! As a beginner, I was struggling to change text color of anchor links. Had to look at Kevin's code on github for the solution. It turned our than css 'color: inherit;' on each anchor link (there is a class for each like) solves it.

  • @Fliteska
    @Fliteska 5 ปีที่แล้ว +3

    Also wondered how sites did that, figured it would be much more than that! Will definitely try this out.

  • @adrian20065
    @adrian20065 5 ปีที่แล้ว +1

    Awesome! This is really practical and something I can use - credit to Kevin Powell.

  • @KarolD666
    @KarolD666 3 ปีที่แล้ว

    This saved the last hair I have after trying to fix a previous "successful attempt" of background switching. Thank you.

  • @zakariaelkhachia1533
    @zakariaelkhachia1533 4 ปีที่แล้ว

    Thanks Kevin! Been looking for this for quite sometime!

  • @gabeadams2926
    @gabeadams2926 5 ปีที่แล้ว +2

    Thanks for sharing this Kevin! Much appreciated :)

  • @rebelmachine88
    @rebelmachine88 5 ปีที่แล้ว

    Fantastic series and a great video, Kevin!

  • @aqua123670
    @aqua123670 5 ปีที่แล้ว

    Yess ! have been waiting for this. Thank you

  • @pallavisingh62
    @pallavisingh62 2 ปีที่แล้ว

    These videos are so helpful! Thank you for making them!

  • @ikhyeonkim4372
    @ikhyeonkim4372 5 ปีที่แล้ว +9

    This is awesome! You’ve got everything that I looked for.

  • @devine_noise
    @devine_noise 2 ปีที่แล้ว

    Had some problems with Gulp CLI on my Windows machine. If you're running into an issue with 'gulp watch' not working, you can make the scss file a css file, delete the package.json, node_modules, package-lock.json, and just remove all "&" (&:after becomes .nav__link::after) in the css and replace it with the parent css class and use Live Server extension to get auto page reload in the browser. Clean up your curly brackets in the CSS and everything works. Thanks for making this Kevin, love your teaching style.

  • @deniskotov
    @deniskotov 3 ปีที่แล้ว

    Ohhhhh!!!! This trick solved so many problems for me! Thank you, Kevin!

  • @_vikramhegde_
    @_vikramhegde_ 5 ปีที่แล้ว

    I'm in love with Intersection Observer.. Just amazing 👍👍👌

  • @ARTICFR0ST
    @ARTICFR0ST 5 ปีที่แล้ว

    Didnt think of this idea until you made a video about it. Thanks for thinking ahead haha

  • @suhrobmuboraksho
    @suhrobmuboraksho 2 ปีที่แล้ว

    Thanks Kevin. Great tutorial!

  • @jamesa.
    @jamesa. 4 ปีที่แล้ว

    Thanks a lot! I didnt even need to watch the intro video and I fully understood!

  • @NERO-ez1mn
    @NERO-ez1mn 3 ปีที่แล้ว

    seriously the 1st vid of theory is difficult for me to understand maybe because i'm a newbie but after this part i see it clearly that we are talking about the observe() native function all along hahahahahaha THANK YOU REALLY SO MUCH you deserve more subs.

  • @tarekjamanlabu9773
    @tarekjamanlabu9773 2 ปีที่แล้ว

    Finally figured it out, thanks 'The King of CSS' ❤️

  • @pauljohn3898
    @pauljohn3898 3 ปีที่แล้ว

    Ever wondered How this comes true and your video Help a lot!

  • @utkarshkukreti239
    @utkarshkukreti239 3 ปีที่แล้ว

    Thank you so much. Great. Root margin concept was very sexy!

  • @danielChibuogwu
    @danielChibuogwu 2 ปีที่แล้ว

    Thanks for this series

  • @philkewley
    @philkewley 2 ปีที่แล้ว

    Every time I start a new project I find myself having to go back through your library of videos, gradually what I am learning is starting to stick, although I still struggle to remember even the most simple instructions! - one thing from this video, is how are you changing the background image in the big hero section, I swear every time I look at the video it's a different Unsplash image but I can't find how you're doing it within the CSS. Thanks as always for sharing

  • @k.jflacko5720
    @k.jflacko5720 2 ปีที่แล้ว

    This was really helpful!

  • @andersonalmeydat7715
    @andersonalmeydat7715 2 ปีที่แล้ว

    amigo eres mi héroe muchas gracias, pude cambiar totalmente el estilo del nav y hacer un super efecto, buen tuto!!

  • @joshuatao7020
    @joshuatao7020 5 ปีที่แล้ว

    Your videos are always amazing!Thank you,sir ❤️❤️❤️

  • @giovannipizzato6888
    @giovannipizzato6888 4 ปีที่แล้ว

    Helped me a lot, thanks man!

  • @DamianDemasi
    @DamianDemasi 3 ปีที่แล้ว

    Great tutorial! Thanks!

  • @jvoyager1
    @jvoyager1 4 ปีที่แล้ว

    This is awesome, thank you!

  • @MegaPupizoid
    @MegaPupizoid ปีที่แล้ว

    Thanks you help me to learn Javascripts from scratch knowledge 😀

  • @greysaga
    @greysaga 2 ปีที่แล้ว

    thank you for this great tutorial!

  • @christianaltan4913
    @christianaltan4913 3 ปีที่แล้ว

    Great Video! really really useful

  • @SaleKnezevic
    @SaleKnezevic 5 ปีที่แล้ว

    Great work, Thank you.

  • @BarthObiefuna
    @BarthObiefuna 3 ปีที่แล้ว

    You are a good teacher.

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt 5 ปีที่แล้ว +1

    Thank u for this awesome video

  • @DamienPeden
    @DamienPeden 5 ปีที่แล้ว

    I just did a menu shrink effect on scroll and its super buggy! This definitely fixes those issues! Thanks!

  • @AllenMarsam
    @AllenMarsam 3 ปีที่แล้ว

    Thanks for your tutorial

  • @kostyantinbilyznyi3496
    @kostyantinbilyznyi3496 4 ปีที่แล้ว

    Thanx! You helped me a lot!

  • @gabiold
    @gabiold 4 ปีที่แล้ว

    Awesome! You could get the header's offsetHeight to set the options.

  • @misterkilluminati6949
    @misterkilluminati6949 2 ปีที่แล้ว

    beautifully done

  • @Darival92
    @Darival92 5 ปีที่แล้ว

    Great videos kevin

  • @mrmamun5830
    @mrmamun5830 5 ปีที่แล้ว +5

    Just amazing! I'm going to copy those JS codes 😁

  • @underdog3864
    @underdog3864 4 ปีที่แล้ว +4

    Epic! I was trying to do it the old way with offsetTop. This is so much easier

  • @robertwattner
    @robertwattner 3 ปีที่แล้ว

    Now we need to offset anchor links to accommodate the sticky header height. Not the margin-top: -100px: padding-top: 100px; to every anchor
    Awesome video btw! Your videos are the best!

  • @zachfenton608
    @zachfenton608 3 ปีที่แล้ว +1

    Perfect just was i was looking for, ill sleep and apply it to my site

  • @royhyde8842
    @royhyde8842 2 ปีที่แล้ว

    That was a lot of fun

  • @saydurrahman1899
    @saydurrahman1899 5 ปีที่แล้ว

    Can you please make a video on responsive Mega menu with really nice animation. This series has been absulutly amazing. I am Learning so much. Thank you for your hard work.

  • @upsylondeveloppement8837
    @upsylondeveloppement8837 5 ปีที่แล้ว

    Great ! Thanx Kevin ;-)

  • @alinawaz4034
    @alinawaz4034 5 ปีที่แล้ว +1

    Very good!!!

  • @Juan-lj4pi
    @Juan-lj4pi 4 ปีที่แล้ว

    You're awesome dude

  • @sofienebb
    @sofienebb 2 ปีที่แล้ว

    Ya it cuming after three years, but you have helped me a lot, so thank you ;)

  • @franco-cespi
    @franco-cespi 3 ปีที่แล้ว

    I love it!

  • @tux8345
    @tux8345 2 ปีที่แล้ว

    wow, you save my life!

  • @majgajsek7818
    @majgajsek7818 2 ปีที่แล้ว

    Thank you good sir :D

  • @jamjam3448
    @jamjam3448 ปีที่แล้ว

    Thank you ☺️

  • @truedivan
    @truedivan 4 ปีที่แล้ว

    Tnx from Russia!) Great and simple

  • @sithumdasantha3147
    @sithumdasantha3147 3 ปีที่แล้ว

    Thanks Man 🤍

  • @GiancarloCarccamo
    @GiancarloCarccamo 2 ปีที่แล้ว

    great video

  • @mrrolandlawrence
    @mrrolandlawrence 3 ปีที่แล้ว

    smashing series. am literally deleting massive of legacy code from projects :)

  • @rahibharoon7852
    @rahibharoon7852 3 ปีที่แล้ว

    Kevin Powell you great really like you videos it's very informative and technical and easy to understand for us. can we use intersection observer API in Reactjs?

  • @noahsscott
    @noahsscott 5 ปีที่แล้ว +1

    Just found your channel. Thank you for these! Would be interested to see intersection observer used for a table of contents say for a wiki or one-pager. I'm trying to assemble one myself with IO but struggling.

    • @vijaydhravida
      @vijaydhravida 5 ปีที่แล้ว

      Any luck on this front @noahScott ? I am trying the same, so if you have any insights I would very much be interested to know.

  • @iliankarasimirov9685
    @iliankarasimirov9685 5 ปีที่แล้ว

    thanks, I just need that

  • @whatthefunction9140
    @whatthefunction9140 3 ปีที่แล้ว

    Nice sass

  • @kensleylewis
    @kensleylewis 3 ปีที่แล้ว

    In connection to this great exercise, is it possible to a parallax effect with this technique (i.e. as you scroll down the page the background image scrolls slower)?

  • @RyanEwanchuk
    @RyanEwanchuk 3 ปีที่แล้ว +2

    Got a question for you. This works great, but it only works for one section. Let's say I have a bunch of sections with a class called "black-section" and I wanted the navigation to change every time it intersected with one of those sections, what code would I have to change?

  • @WebDeveloper682
    @WebDeveloper682 9 หลายเดือนก่อน

    perfect!

  • @Franiveliuselmago
    @Franiveliuselmago 4 ปีที่แล้ว

    I'm trying to find the video about how to highlight the menu element of the current section. Help please.

  • @decartier23designs68
    @decartier23designs68 5 ปีที่แล้ว

    Hi there, I really like your work and i was wondering if you can tell me how can i do that 3D modern look product gallery or slider for websites , the one that the photo has no background ? is it like just a png photo or do you have to add some more edit to make it look like that . Thankyou !

  • @akanshamotwani3646
    @akanshamotwani3646 4 ปีที่แล้ว

    Hi Kevin, how would the intersection observer work with position sticky of CSS. Instead of the elements in the viewport, I need to observe the element which currently is stick on the top. Amazing series though!

  • @faicalbahsis5659
    @faicalbahsis5659 4 ปีที่แล้ว

    thaankuu so much brooh

  • @abdillahsyafiq
    @abdillahsyafiq 3 ปีที่แล้ว

    can't wait to implement this in my projects!

  • @timhart6061
    @timhart6061 3 ปีที่แล้ว

    Hi! I was wondering where do u put transition on when u scroll back up? I put transition on the .nav-scrolled only so when i scroll down the transition works but when i scroll back up it just removes instant

  • @Billiam112
    @Billiam112 4 ปีที่แล้ว

    That's cool, thanks for sharing! Apparently, it is not supported by ie11, so my question to you is: would you go for listening to scroll event if you had to support ie11 or would you do it in some other way?

  • @mahvaree
    @mahvaree 2 ปีที่แล้ว

    Thanks

  • @iUmerFarooq
    @iUmerFarooq 5 ปีที่แล้ว

    Sir, make a video on how the content fadeout when we scroll down with animations like content come from left or right. Thanks

  • @missinglink2416
    @missinglink2416 3 ปีที่แล้ว

    awesome.. just subscribe!!!

  • @DanSinor
    @DanSinor 4 ปีที่แล้ว

    What would the "const sectionOne = document.querySelector(".home-intro");" be if I wanted to target and trigger off of section ID (services) rather than CLASS (bg-dark)? As in example (). Would it use ("#services")?

  • @pasizdobrekuce
    @pasizdobrekuce 4 ปีที่แล้ว

    Nice one. Works great, but what when there's not intro section in DOM?

  • @abdulrehaman2406
    @abdulrehaman2406 5 ปีที่แล้ว +1

    Are you a mind reader or what 😅 I was stuck on this topic today and wanted to find a solution. I open TH-cam and bam ! There is your video to fix my problem 😍 This happened thrice till now 👍🏻

    • @KevinPowell
      @KevinPowell  5 ปีที่แล้ว +1

      Haha, always nice to time things perfectly :D

    • @eduardopassero47
      @eduardopassero47 5 ปีที่แล้ว

      Same for me!

  • @cano5063
    @cano5063 4 ปีที่แล้ว

    so the advantage of intersection observer instead of event-listeners is that it is faster at loading?

  • @dimensionalpotential
    @dimensionalpotential 4 ปีที่แล้ว

    But Kevin et al, if I need to differentiate between scrolling down and scrolling up - is there an alternative to a JS scroll watcher in that case?

  • @nicofchile
    @nicofchile 4 ปีที่แล้ว

    Good video, and good series. I use the function described but im having problems make it responsive. How can i, stop the script for responsive. i tried to condition the function to innerWidth, but i failed. I'll really appreciate any idea.

  • @NoTengoIdeaGuey
    @NoTengoIdeaGuey 2 ปีที่แล้ว

    Take a drink every time he says "I've a got another video for that subject already" and you'll be as drunk as i afgbnq

  • @kolaskid3210
    @kolaskid3210 4 ปีที่แล้ว

    Hi Kevin, how do I change my default browser pls? in gulpfile browserSync. Thanks

  • @MrHerbalite
    @MrHerbalite 5 ปีที่แล้ว

    Great demo. Thank you.
    How do I accomplish the same if the intersection is actually a sibling element in a wrapper class that has max-height set and overflow is set to auto? I do use CSS grid. The wrapper class has a scrolling area. The first entry within is sticky and everything is fine unless I want to jump to the next entry through an external link. As you probably know, the sibling entries are getting pushed under the sticky part.
    So far the advice that I got is: Can't be done (aside from working with the scroll event), but I'm not quite ready to accept that.
    If the intersectionObserver can watch out for two siblings to overlap each other my problem can be solved. Any idea, or even a tutorial?

    • @KevinPowell
      @KevinPowell  5 ปีที่แล้ว

      You can change the "root" in the options, so instead of looking at the viewport, it's looking at an element on yoru page.

  • @valentinfigueiredo
    @valentinfigueiredo 3 ปีที่แล้ว

    hey ! thanks for the video! i want to know if its possible to do without sass? thanks

  • @J90JAM
    @J90JAM 3 ปีที่แล้ว

    Why CSS Variables and not SCSS Variables? Another awesome video.

  • @acai4171
    @acai4171 3 ปีที่แล้ว

    Weird, for me it adds the class but the new CSS properties don't apply to it.
    Also, I when I exit the section again, the class isn't being removed. Any ideas?

  • @magnuslundstrm3719
    @magnuslundstrm3719 5 ปีที่แล้ว

    Hi Kevin, thanks for the great tutorials. I have now watched both ur introducting video and this video. In the first video you show how to make it interact with sections and change background colors etc. In this it is the header. I wondered however how i can combine does two scripts into one, so I can use both features on one site.
    Kind regards
    Magnus

    • @KevinPowell
      @KevinPowell  5 ปีที่แล้ว

      We need to have 2 observers, as we are watching for different things. At least I think so....

  • @Danteuchija1
    @Danteuchija1 4 ปีที่แล้ว

    Can you make a video about changing the background of the navbar link depending on the section? If I’m scrolling down to the contact section, then the link that point to that section will change its background color to show the user where in the page they are.

  • @iUmerFarooq
    @iUmerFarooq 5 ปีที่แล้ว

    How I will understand this JS code or strong my JS code work flow please tell me, anyone?