Smooth Scroll to an Element in React

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2025

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

  • @colbyfayock
    @colbyfayock  5 หลายเดือนก่อน

    Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course

  • @gerardsiles
    @gerardsiles 3 หลายเดือนก่อน

    Every time I need to do something new on the front end side that I haven't done before, I find a video where you explain it. You are amazing, and I'm extremely grateful for you sharing your knowledge and the way that you do it.
    Blessings!

    • @colbyfayock
      @colbyfayock  3 หลายเดือนก่อน

      appreciate the kind words! let me know if you search for something and can't find good content ;) always looking for new ideas

  • @tocube1
    @tocube1 9 หลายเดือนก่อน +1

    Buttery smooth scroll with buttery smooth explanations and all the little details and references, on point and without wasting time.
    thanks man. top notch content 🤘

    • @colbyfayock
      @colbyfayock  9 หลายเดือนก่อน +1

      Thanks for the kind words! Glad it was helpful 🙌

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

    Always wonderful stuff from you, Colby! I love when you cover browser APIs like this.

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

      thanks alex! definitely want to explore more in this area, think its especially interesting with all the cool APIs that have been popping up lately

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

    Your voice is so calming, awesome tutorial

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

    Simple, quick, easy to understand! Great video! Cannot wait to check the rest of them! +1 sub!

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

      thank you!

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

    You could also use `scroll-behavior: smooth;` CSS declaration at 0:27 and get rid of all this JS complexity.

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

      great solution!! definitely useful to have different options available depending on the use case

  • @MarinaEbbecke
    @MarinaEbbecke 4 หลายเดือนก่อน

    Thank-you for the video! Simple and elegant solution and very well explained.

    • @colbyfayock
      @colbyfayock  4 หลายเดือนก่อน

      no problem!

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

    Are you also able to smooth scroll with an href?

  • @LucasGabrieldaSilva-bt1co
    @LucasGabrieldaSilva-bt1co 9 หลายเดือนก่อน

    Thanks Colby, you are a true hero

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

      no problem 🫡

  • @piotrszczesniak685
    @piotrszczesniak685 5 หลายเดือนก่อน

    I needed smooth scroll together with a pagination, so whenever I changed the page scrollIntoView() couldn't work as expected. What I've done is this
    sectionRef.current?.scrollIntoView({
    behavior: 'smooth',
    block: 'start',
    });
    setTimeout(() => {
    setCurrentPageNum((curr) => curr + 1);
    }, 500);
    I've basically time-outed the execution of setCurrectPageNum() after the scrollIntoView took place.

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

    Thanks Colby, loved this!

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

      no problem! glad you found it helpful 😁

  • @mateusloubach
    @mateusloubach 8 หลายเดือนก่อน

    wow.. I've been looking forever for you my guy..

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

    Hi from Argentina! The very end of South America.
    Despite the topic of this video is awesome by itself I can see that you have an excellent React Typescript domain.
    Do you have any plan for tutorials about it?
    Sorry if my English looks kind of weird 😂

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

      hey there! i dont know that i have any Typescript-specific tutorials planned, is there something specific you'd like to see?

  • @bagel-minecraft-videos
    @bagel-minecraft-videos 6 หลายเดือนก่อน

    But what if div that i want scroll to located in another component from scroll button

    • @colbyfayock
      @colbyfayock  6 หลายเดือนก่อน

      you could use a standard ID string to handle that, or you can lift up the ID to the lowest common parent but that may be a lot depending on the use case

    • @bagel-minecraft-videos
      @bagel-minecraft-videos 6 หลายเดือนก่อน

      @@colbyfayock thanks !

  • @Rando327
    @Rando327 8 หลายเดือนก่อน

    this is cool I want to implement it in my app. But I have the header in my App.tsx, and I have two sections (each in a component) and I want the user to jump to from buttons in the header. how can I do that?

  • @arturfreire3410
    @arturfreire3410 5 หลายเดือนก่อน

    It helped me a lot! Thanks.

    • @colbyfayock
      @colbyfayock  5 หลายเดือนก่อน

      no problem!

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

    Awesome video man 👏 Subbed 🎉

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

    Great video, so well explained

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

      Thank you!

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

    How do you do it when the div where you want the page to scroll is inside another component?

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

      hey im not totally following, any chance you hvae an example?

  • @quanghungpham269
    @quanghungpham269 11 หลายเดือนก่อน

    This is cool man.
    I actually tried these scroll methods, and my experience is that they cannot be used along with "scroll" EventListnener. Because it's provoked every time I scroll, the section will freeze and I cannot scroll to different section. It's also doesn't work with behavior: smooth too.

    • @colbyfayock
      @colbyfayock  11 หลายเดือนก่อน

      yeah thats an interesting one as that's kind of what I would expect, in that the scrolling action would still fire a scroll listener. does the instant experience not also triger it? a quick test in my console log just playing around on this youtube page and .scroll(0,1000) triggered an event with window.addEventListener('scroll'...)
      i just did a quick search and not seeing anything about triggering on finish or any kind of given time for it to finish which is apparently not constant to spec
      also looks like the event doesnt have any unique information about what triggered it
      feels a bit complicated but perhaps before triggering, you can store a ref that states you're going to perform the scroll then once you get the the scroll location, the scroll listener sets that back off or something. not sure 🤔

  • @KSalvatore
    @KSalvatore 8 หลายเดือนก่อน

    Can you also tell how can i get hash from url then scroll to div with same hash id. I want to use it nextjs. I found many solution and tried some tricks nothing works perfectly. Please

    • @colbyfayock
      @colbyfayock  8 หลายเดือนก่อน

      window.location.hash gives you that value but not sure if thats what you mean?

    • @KSalvatore
      @KSalvatore 8 หลายเดือนก่อน

      @@colbyfayock yeah i will hash like that but the scrolling was giving headache. The app router, scroll to top on link click, link on same page and different page, router refresh all these was caused problems. Now somehow it's working as intended.
      Thanks by the way

  • @AlexLozada32
    @AlexLozada32 8 หลายเดือนก่อน +2

    Why not use the traditional div id="contact" and then "a" tag with the href="#contact" and in the main index.css file just put at the universal selector * { scroll-behavior: smooth }. It works exactly the same.

    • @colbyfayock
      @colbyfayock  8 หลายเดือนก่อน

      also a good solution!

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

    Hi space jelly and thanks. You do have a good voice.

  • @asifrahman907
    @asifrahman907 7 หลายเดือนก่อน

    please make a video how we can navigate to a particular page onClick the navbar element

    • @colbyfayock
      @colbyfayock  7 หลายเดือนก่อน

      do you mean page routing and links to those pages? you might be interested in this video i did where i used Wouter: th-cam.com/video/nvGhGSmEQZU/w-d-xo.html

  • @dev_insights783
    @dev_insights783 4 หลายเดือนก่อน +2

    I just use scroll-behaviour: smooth; that's it

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

    Get fresh tutorials and other free content straight to your inbox! colbyfayock.com/news

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

      Just subscribed!

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

    This is the kind of short tutorial that never works when u really need it in project.

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

      did this not work somewhere?... its a browser API, the only way it wouldn't be available is if you're using a browser that doesn't support the API, is that the case? it has pretty good support... caniuse.com/?search=scrollintoview

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

      @@colbyfayock I was saying, try this example works perfect... if tomorrow I stuck on problem like this, which varies from the implementation, I will lose 3 days to make it work... happening most of the time...