Simple single-page smooth scrolling

Поделиться
HTML-код
  • Опубликовано: 17 янв 2023
  • Code: codepen.io/kevinpowell/pen/eY...
    #css
    --
    Come hang out with other dev's in my Discord Community
    💬 / discord
    Keep up to date with everything I'm up to
    ✉ www.kevinpowell.co/newsletter
    Come hang out with me live every Monday on Twitch!
    📺 / kevinpowellcss
    ---
    Help support my channel
    👨‍🎓 Get a course: www.kevinpowell.co/courses
    👕 Buy a shirt: teespring.com/stores/making-t...
    💖 Support me on Patreon: / kevinpowell
    ---
    My editor: VS Code - code.visualstudio.com/
    ---
    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.
    Twitter: / kevinjpowell
    Codepen: codepen.io/kevinpowell/
    Github: github.com/kevin-powell
    ---
    And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

Комментарии • 106

  • @KevinPowell
    @KevinPowell  Год назад +53

    The code: codepen.io/kevinpowell/pen/eYjRVmw

    • @tootalldan5702
      @tootalldan5702 Год назад +2

      Nice. Now get RUclips Web Page to make a button (up and down) on there screen for moving down one set of Vids.

  • @Ed-gd5ec
    @Ed-gd5ec Год назад +128

    Please bring more and more of such powerfull shorts of CSS/JS.
    Thanks

  • @nagarajansubramani
    @nagarajansubramani Год назад +28

    What I really learnt from this is that you can manipulate CSS variables from Javascript, and how to. 🙏

  • @sgbbco3981
    @sgbbco3981 7 месяцев назад +2

    Hands down some of the best if not the best shorts on YT. Thank you for always offering up useful tidbits that are actually applicable in daily life.

  • @cliffordnelson9249
    @cliffordnelson9249 Год назад +53

    oh!!! you can use JS to set CSS Vars 🤯😄 I needed this, Thank you!!

    • @patricknelson
      @patricknelson Год назад +4

      Yes, and one awesome superpower: It’s reactive. That is, every var() utilizing that custom attribute/var will immediately respond to those changes. 🔥

    • @ayviuon
      @ayviuon Год назад +1

      You can even use PHP for that it you want.

    • @Konyad
      @Konyad 6 месяцев назад +1

      ​@@ayviuonPHP runs on a server, how would you like to change a client-side variable?

  • @walky4158
    @walky4158 4 месяца назад

    When one short defeats 99% of the pages who mislead you for this same concept! DUDE you are awesome!

  • @andresmorantes6462
    @andresmorantes6462 10 месяцев назад

    I love you Kevin I spent a lot of hours playing with the JS Scroll events and then you save my day using CSS, Thank you very much

  • @shgysk8zer0
    @shgysk8zer0 5 месяцев назад

    I just have my height controlled by the same custom property that also sets scroll padding.

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

    No matter how familiar or new I am to something I always find your videos useful and entertaining. Thanks for sharing. Cheers

  • @balogunmuhammadawwal1316
    @balogunmuhammadawwal1316 Год назад +1

    Thanks Kevin, you have always been there for me 🙂

  • @danialalfayyadh2524
    @danialalfayyadh2524 5 месяцев назад

    thankyou sir, you’re rock

  • @mixxxer
    @mixxxer 25 дней назад

    I had no idea you could set css variable values with js. That is super powerful.

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

    You're a life saver Kevin! This was exactly what I needed!

  • @omarnur2269
    @omarnur2269 Год назад +1

    This guy understands our current problems 🪄🪄 Keep going Kevin .

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

    Dude is a legit pro. I don't just mean at web dev, but teaching it as well.

  • @BMikel
    @BMikel Год назад +1

    Thats absolutely awesome

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

    You are a life saver man, I was stragling with this problem for days
    Thank you

  • @gurmaansidhu8515
    @gurmaansidhu8515 7 месяцев назад +1

    thank you mate, have a good day ❤️

  • @siegetank316
    @siegetank316 Год назад +1

    Thank you for this!

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

    I needed this a few months ago. 😂. Great tip!

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

    Yes! More devs need to know about this. Another great reason to support this is to ensure #hash links work seamlessly with your sticky headers. All too often I see sites linking to positions within the page that are _confusingly_ not visible or otherwise overlapped by their (also probably too big) sticky header.

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

    I recently ran into this problem and thought there was no solution for it. You sir are a wizard.

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

    This is the exact problem I was going through. You are genius boss

  • @Tofu-it8mm
    @Tofu-it8mm 4 месяца назад

    Thanks!!!!! Just searching for this problem

  • @CERO12345
    @CERO12345 6 месяцев назад

    Finally CSS with Js!

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

    Thanks a lot, scroll-padding helped! I need to study the JS approach in more detail but so far the padding is good enough for my purpose.

  • @Mioscape
    @Mioscape Год назад +1

    Thankyou

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

    Never heard of scroll-padding before - what a great solution!

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

    Excellent video! Thank you

  • @hikari1690
    @hikari1690 Год назад +1

    Beautiful. Gone are the day of fiddling with offsetx like a newbie

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

    exactly figuring it out and here is kevin doctor strange.

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

    This was perfect and so needed! 🎉

  • @Nikox102
    @Nikox102 6 месяцев назад

    thank you works amazing

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

    Wow thanks Kev 🙏🏼

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

    Since I’ve started messing around with JS, it truly transformed my point of view over CSS, it makes it sooo much more powerful, even though CSS3 really is powerful on its own, JS brings it to the next level

  • @AbleMagbegor-rj8jw
    @AbleMagbegor-rj8jw 8 месяцев назад

    wow this was very helpful thanks

  • @roniteletor6971
    @roniteletor6971 10 месяцев назад

    oh my gooodddddd!!!!!!!!!!!! thank very much this helped me a lot.

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

    Wow. That's really cool. I'm sending myself this video. 👍👍

  • @SOSMENAJAYBIEE
    @SOSMENAJAYBIEE 5 месяцев назад

    thanks

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

    You are a genius

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

    Thanks 😌😌 it's very helpful ☺️

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

    I’ve had this issue with the scroll being to far last night and I said I’ll figure it out tomorrow. I’m so glad I found my answer without even looking for it

    • @jakes-dev1337
      @jakes-dev1337 Год назад

      try just adding an empty div above each section. scroll to that instead.

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

    You re such a genius 🔥🔥❤️

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

    Magical!

  • @doum5792
    @doum5792 27 дней назад

    And now you also need to handle this on resize otherwise if mobile menu height changes on mobile, you might scroll-padding the wrong value.

  • @eternallyMarsh
    @eternallyMarsh 3 месяца назад +1

    Or you can just set the scroll-padding to the same height you set for the navbar

  • @TheMotivatorYouNeed-uj1jx
    @TheMotivatorYouNeed-uj1jx 6 месяцев назад

    this is good stuff.

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

    brilliant

  • @COCRidaaa
    @COCRidaaa 7 месяцев назад

    NICE

  • @phim4297
    @phim4297 Год назад +1

    Nice

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

    You are best😩🤲❤️

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

    OMG this is my problem 2 years ago ;-;. Anyways, thanks for sharing sir Kevin!

  • @user-kb3zt3cv9s
    @user-kb3zt3cv9s 6 месяцев назад

    shukran

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

    🤯

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

    Wow

  • @thenewyorkean
    @thenewyorkean 10 месяцев назад

    nice, thanks Kevin! Is there a way that we can do sth in "reverse" with scroll height: to make a string (a title lets say) to change dynamically depending on the section the user sees ? So when her screen is at "Section 4", there's a title who changes accordingly ?
    Have you done sth like already? Thanks in advance man

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

    wow

  • @seekhokhushise
    @seekhokhushise 11 месяцев назад

    Found helpful something

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

    You are a crack, thank you so much!!!!!

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

    Thx! How about highlighting nav links on hover?

  • @SumaArunVadakannavar
    @SumaArunVadakannavar Месяц назад

    awesome, its single line of code change and i was trying it since twodays to fix this.

  • @brunofelipe5380
    @brunofelipe5380 Месяц назад

    thanks, i was trying apply scroll-behavior on body tag and wasnt work

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

    Awesome. Can you this example with resizeObserver to update the padding if header is changed bei resizing? Think that would be interesting and makes it responsive too

  • @edylopez3891
    @edylopez3891 9 месяцев назад

    Where can I find the full video about This. Thanks

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

    Nice video

  • @preskoeducation7464
    @preskoeducation7464 4 месяца назад

    How to make scrolling from the mousewheel smooth without a library?

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

    Great idea to set that custom value via JavaScript but have a fallback

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

    W algo

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

    But is it good for SEO? Since there’s some JavaScript involved

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

    wow, scroll padding must be new

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

    What if someone links directly to a section target? How can I engage the code then?

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

    About this, when you click a href link, it changes the url name as well. How do you somehow save the original domain name somewhere permanently, localstorage changes the name everytime the user reloads so it won't work for me

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

    I see devs giving each section a 100vh, so when you hit the link and scrolls the section covers the viewport and solves the issue of scrolling and don't get to the middle. Kevin what do you think about this approach?

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

    What about `:target{ scroll-margin-top: 10rem; }`?

  • @007liquidsilver
    @007liquidsilver 5 месяцев назад

    Hello Kevin- do you know how I can create a payment page on my website where I can take payment from clients for items I sell

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

    what if the navbar is being resized?

  • @jakes-dev1337
    @jakes-dev1337 Год назад

    It's much more readable to just scroll to an empty div above each section.

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

    Is there a way to achieve this with especially without the magic number?

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

    What about Firefox?

  • @unnoticedspacegoat8537
    @unnoticedspacegoat8537 Год назад +1

    maybe i am weird but i find smooth scrolling annoying. If i go back and forth a lot between sections this is slow and disorienting like i am on a roller coaster.

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

      maybe faster the scrolling animation i guess

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

      For some people it can be super annoying, and even disorientating, and one reason it's important to turn off animations like this if people have disabled this type of thing in their system... I couldn't squeeze that into this short, but I have talked about it in passing in longer videos, and I should do some longer content on that as well.
      Personally I like it because I find it easier to keep track of where I am on a page.

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

    Maybe I’m blind, but where do you define the value of the --scroll-padding property? I know that you set 5rem as the fallback value, but is that the value it gets, since it has no set value elsewhere?

    • @abderrahmaneelmahmi7749
      @abderrahmaneelmahmi7749 Год назад +1

      The html element

    • @KevinPowell
      @KevinPowell  Год назад +1

      I never defined it, I let the JS do that (in this case, it's applying it to the html element). If the JS fails to load or somethin,, it goes to the fallback value.

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

      @@KevinPowell, of course!
      With nearly six years of experience with HTML and CSS, I should’ve caught that.
      But thanks for answering nonetheless! :)

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

    Why doesn't scroll behavior do anything for me it still just jumps

  • @edigunawan3194
    @edigunawan3194 7 месяцев назад

    hallo Mr. Powell. what this is full youtube ?

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

    there's so much simpler way

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

    You can achieve this without JS by having one CSS variable control nav height and scroll padding

    • @KevinPowell
      @KevinPowell  Год назад +1

      Yeah, but fixed heights are the worst :P. For a nav you can get away with them obviously, but I always tend to avoid them when I can.

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

      @@KevinPowell yeah ur right. JS is probably the best solution here

  • @scottonanski4173
    @scottonanski4173 23 дня назад

    Great solution, but why make this a short? I have to keep re-watching it so I can rewind to the see the long strings. lol
    I want to pull my eyeballs out.
    Definitely the wrong format for code...

  • @kirillium9332
    @kirillium9332 9 месяцев назад

    Это просто охуенно!

  • @mccall7122
    @mccall7122 3 месяца назад

    The js part of this seems very hacky.

  • @Softie466
    @Softie466 6 месяцев назад

    V

  • @sumaisumai1939
    @sumaisumai1939 7 месяцев назад

    Fake 😡

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

    Nice

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

    wow