How to change your navigation style on scroll

Поделиться
HTML-код
  • Опубликовано: 14 июн 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 RUclips 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 лет назад +102

    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. RUclips 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!!!

  • @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.

  • @muhammadraza2940
    @muhammadraza2940 2 года назад +1

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

  • @cervece41
    @cervece41 4 года назад +1

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

  • @MARVTOBI-wz8qk
    @MARVTOBI-wz8qk 9 месяцев назад

    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 :)

  • @KarolD666
    @KarolD666 3 года назад

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

  • @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

  • @adrian20065
    @adrian20065 5 лет назад +1

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

  • @zakariaelkhachia1533
    @zakariaelkhachia1533 4 года назад

    Thanks Kevin! Been looking for this for quite sometime!

  • @pallavisingh62
    @pallavisingh62 2 года назад

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

  • @gabeadams2926
    @gabeadams2926 5 лет назад +2

    Thanks for sharing this Kevin! Much appreciated :)

  • @rebelmachine88
    @rebelmachine88 5 лет назад

    Fantastic series and a great video, Kevin!

  • @Fliteska
    @Fliteska 5 лет назад +3

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

  • @_vikramhegde_
    @_vikramhegde_ 5 лет назад

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

  • @ikhyeonkim4372
    @ikhyeonkim4372 5 лет назад +9

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

  • @suhrobmuboraksho
    @suhrobmuboraksho 2 года назад

    Thanks Kevin. Great tutorial!

  • @aqua123670
    @aqua123670 5 лет назад

    Yess ! have been waiting for this. Thank you

  • @deniskotov
    @deniskotov 3 года назад

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

  • @jamesa.
    @jamesa. 4 года назад

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

  • @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!

  • @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.

  • @ARTICFR0ST
    @ARTICFR0ST 5 лет назад

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

  • @MegaPupizoid
    @MegaPupizoid Год назад

    Thanks you help me to learn Javascripts from scratch knowledge 😀

  • @danielChibuogwu
    @danielChibuogwu Год назад

    Thanks for this series

  • @DamianDemasi
    @DamianDemasi 2 года назад

    Great tutorial! Thanks!

  • @BarthObiefuna
    @BarthObiefuna 3 года назад

    You are a good teacher.

  • @utkarshkukreti239
    @utkarshkukreti239 3 года назад

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

  • @giovannipizzato6888
    @giovannipizzato6888 3 года назад

    Helped me a lot, thanks man!

  • @joshuatao7020
    @joshuatao7020 4 года назад

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

  • @greysaga
    @greysaga 2 года назад

    thank you for this great tutorial!

  • @k.jflacko5720
    @k.jflacko5720 2 года назад

    This was really helpful!

  • @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.

  • @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.

  • @andersonalmeydat7715
    @andersonalmeydat7715 2 года назад

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

  • @christianaltan4913
    @christianaltan4913 3 года назад

    Great Video! really really useful

  • @AllenMarsam
    @AllenMarsam 2 года назад

    Thanks for your tutorial

  • @mrmamun5830
    @mrmamun5830 5 лет назад +5

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

  • @jvoyager1
    @jvoyager1 4 года назад

    This is awesome, thank you!

  • @zachfenton608
    @zachfenton608 3 года назад +1

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

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt 5 лет назад +1

    Thank u for this awesome video

  • @misterkilluminati6949
    @misterkilluminati6949 2 года назад

    beautifully done

  • @SaleKnezevic
    @SaleKnezevic 5 лет назад

    Great work, Thank you.

  • @moczikgabor
    @moczikgabor 4 года назад

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

  • @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

  • @DamienPeden
    @DamienPeden 4 года назад

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

  • @kostyantinbilyznyi3496
    @kostyantinbilyznyi3496 4 года назад

    Thanx! You helped me a lot!

  • @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!

  • @underdog3864
    @underdog3864 4 года назад +4

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

  • @Darival92
    @Darival92 5 лет назад

    Great videos kevin

  • @royhyde8842
    @royhyde8842 2 года назад

    That was a lot of fun

  • @jamjam3448
    @jamjam3448 Год назад

    Thank you ☺️

  • @truedivan
    @truedivan 3 года назад

    Tnx from Russia!) Great and simple

  • @sofienebb
    @sofienebb 2 года назад

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

  • @alinawaz4034
    @alinawaz4034 5 лет назад +1

    Very good!!!

  • @majgajsek7818
    @majgajsek7818 2 года назад

    Thank you good sir :D

  • @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.

  • @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?

  • @tux8345
    @tux8345 2 года назад

    wow, you save my life!

  • @upsylondeveloppement8837
    @upsylondeveloppement8837 5 лет назад

    Great ! Thanx Kevin ;-)

  • @rahibharoon7852
    @rahibharoon7852 2 года назад

    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?

  • @Juan-lj4pi
    @Juan-lj4pi 4 года назад

    You're awesome dude

  • @sithumdasantha3147
    @sithumdasantha3147 3 года назад

    Thanks Man 🤍

  • @RyanEwanchuk
    @RyanEwanchuk 2 года назад +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?

  • @noahsscott
    @noahsscott 4 года назад +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 4 года назад

      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.

  • @GiancarloCarccamo
    @GiancarloCarccamo 2 года назад

    great video

  • @franco-cespi
    @franco-cespi 3 года назад

    I love it!

  • @iliankarasimirov9685
    @iliankarasimirov9685 5 лет назад

    thanks, I just need that

  • @mrrolandlawrence
    @mrrolandlawrence 3 года назад

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

  • @pasizdobrekuce
    @pasizdobrekuce 4 года назад

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

  • @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!

  • @mahvaree
    @mahvaree 2 года назад

    Thanks

  • @valentinfigueiredo
    @valentinfigueiredo 3 года назад

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

  • @kensleylewis
    @kensleylewis 2 года назад

    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)?

  • @WebDeveloper682
    @WebDeveloper682 8 месяцев назад

    perfect!

  • @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 !

  • @HeyTylerMartin
    @HeyTylerMartin 2 года назад +1

    Are you only using CSS variables for demonstration purposes, or is there another reason you're choosing CSS variables over SASS variables when you're already using SASS? Still learning, just trying to wrap my head around it all 😅

  • @whatthefunction9140
    @whatthefunction9140 2 года назад

    Nice sass

  • @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?

  • @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 RUclips and bam ! There is your video to fix my problem 😍 This happened thrice till now 👍🏻

  • @clpr635
    @clpr635 Год назад

    thanks

  • @arksan8221
    @arksan8221 5 лет назад

    I was really looking forward to enrolling in your Sass course sometime this June... just didn’t get a chance to. And now it seems that enrollment is closed.😭 is there any chance it will be available again? I had it all planned out... I was going to take your course next before I take JavaScript courses 😞

  • @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

  • @missinglink2416
    @missinglink2416 3 года назад

    awesome.. just subscribe!!!

  • @ShawnRitch
    @ShawnRitch 4 года назад

    Instead of using pixels, can ems be used? Thanks for all your tutorials :)

  • @Franiveliuselmago
    @Franiveliuselmago 4 года назад

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

  • @faicalbahsis5659
    @faicalbahsis5659 4 года назад

    thaankuu so much brooh

  • @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

  • @J90JAM
    @J90JAM 3 года назад

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

  • @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.

  • @abdillahsyafiq
    @abdillahsyafiq 3 года назад

    can't wait to implement this in my projects!

  • @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

  • @xades971
    @xades971 3 года назад

    Probably a silly question but how is that border on the nav elements made? Where it starts from the middle and stretches out to the end points. I'm looking through the repo but I can't seem to get my head around it

  • @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.

  • @WTFCACTUS
    @WTFCACTUS 3 года назад

    Im late for this video but also a beginner in programming, i didnt understant 1 thing, is this code vanilla javascript or is it another framework?

  • @Eben_Haezer
    @Eben_Haezer 3 года назад

    do you have any video on how to hide horizontal scrollbars that appear when vertical scrollbar appear. i don't know what i'm doing wrong