Smooth Scroll to an Element in React

  Рет қаралды 27,488

Colby Fayock

Colby Fayock

7 ай бұрын

Learn how to smooth scroll to an element, the top of the page, or a custom location with JavaScript APIs scrollIntoView, scroll, and scrollBy methods.
We'll walk through how to trigger scrolling methods programmatically in a react app including scrolling to an element with scrollIntoView, scrolling to the top of the page with scroll and scrollTo, and scrolling page by page based on dynamic viewport height with scrollBy.
🧰 Resources
Tutorial: spacejelly.dev/posts/how-to-s...
Code: github.com/colbyfayock/my-scr...
Demo: my-scrolling.vercel.app/
📺 KZbin: kzbin.info?sub_c...
🐦 Twitter: / colbyfayock
📹 Twitch: / colbyfayock
✉️ Newsletter: www.colbyfayock.com/newsletter/
🎥 What I Use: www.colbyfayock.com/uses
#colbyfayock #reactjs #ux #webdevelopment

Пікірлер: 49
@colbyfayock
@colbyfayock 7 күн бұрын
Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course
@tocube1
@tocube1 4 ай бұрын
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 4 ай бұрын
Thanks for the kind words! Glad it was helpful 🙌
@TrostCodes
@TrostCodes 7 ай бұрын
Always wonderful stuff from you, Colby! I love when you cover browser APIs like this.
@colbyfayock
@colbyfayock 7 ай бұрын
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
@hqasmei
@hqasmei 7 ай бұрын
Thanks Colby, loved this!
@colbyfayock
@colbyfayock 7 ай бұрын
no problem! glad you found it helpful 😁
@bandekhoda7801
@bandekhoda7801 7 ай бұрын
Your voice is so calming, awesome tutorial
@colbyfayock
@colbyfayock 7 ай бұрын
thank you 😁
@colbyfayock
@colbyfayock 7 ай бұрын
Get fresh tutorials and other free content straight to your inbox! colbyfayock.com/news
@hqasmei
@hqasmei 7 ай бұрын
Just subscribed!
@LucasGabrieldaSilva-bt1co
@LucasGabrieldaSilva-bt1co 4 ай бұрын
Thanks Colby, you are a true hero
@colbyfayock
@colbyfayock 4 ай бұрын
no problem 🫡
@arturfreire3410
@arturfreire3410 14 күн бұрын
It helped me a lot! Thanks.
@colbyfayock
@colbyfayock 12 күн бұрын
no problem!
@lukesoutherton6953
@lukesoutherton6953 4 ай бұрын
Great video, so well explained
@colbyfayock
@colbyfayock 4 ай бұрын
Thank you!
@HarshDoes
@HarshDoes 7 ай бұрын
Awesome video man 👏 Subbed 🎉
@colbyfayock
@colbyfayock 7 ай бұрын
thank you!!
@mateusloubach
@mateusloubach 3 ай бұрын
wow.. I've been looking forever for you my guy..
@colbyfayock
@colbyfayock 3 ай бұрын
🙌
@piotrszczesniak685
@piotrszczesniak685 8 күн бұрын
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.
@Rando327
@Rando327 3 ай бұрын
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?
@fran_sar
@fran_sar 7 ай бұрын
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 7 ай бұрын
hey there! i dont know that i have any Typescript-specific tutorials planned, is there something specific you'd like to see?
@Polynook
@Polynook 7 ай бұрын
You could also use `scroll-behavior: smooth;` CSS declaration at 0:27 and get rid of all this JS complexity.
@colbyfayock
@colbyfayock 7 ай бұрын
great solution!! definitely useful to have different options available depending on the use case
@KingOfCha0z
@KingOfCha0z 4 ай бұрын
How do you do it when the div where you want the page to scroll is inside another component?
@colbyfayock
@colbyfayock 4 ай бұрын
hey im not totally following, any chance you hvae an example?
@quanghungpham269
@quanghungpham269 6 ай бұрын
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 6 ай бұрын
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 🤔
@bagel-minecraft-videos
@bagel-minecraft-videos Ай бұрын
But what if div that i want scroll to located in another component from scroll button
@colbyfayock
@colbyfayock Ай бұрын
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 Ай бұрын
@@colbyfayock thanks !
@eleah2665
@eleah2665 7 ай бұрын
Hi space jelly and thanks. You do have a good voice.
@colbyfayock
@colbyfayock 7 ай бұрын
thank you 😁
@KSalvatore
@KSalvatore 3 ай бұрын
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 3 ай бұрын
window.location.hash gives you that value but not sure if thats what you mean?
@KSalvatore
@KSalvatore 3 ай бұрын
@@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
@asifrahman907
@asifrahman907 2 ай бұрын
please make a video how we can navigate to a particular page onClick the navbar element
@colbyfayock
@colbyfayock 2 ай бұрын
do you mean page routing and links to those pages? you might be interested in this video i did where i used Wouter: kzbin.info/www/bejne/pKeqmXqJoqqEkLc
@AlexLozada32
@AlexLozada32 3 ай бұрын
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 3 ай бұрын
also a good solution!
@MrEnsiferum77
@MrEnsiferum77 7 ай бұрын
This is the kind of short tutorial that never works when u really need it in project.
@colbyfayock
@colbyfayock 7 ай бұрын
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 7 ай бұрын
@@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...
Get Selected Text in React
25:12
Colby Fayock
Рет қаралды 1,8 М.
Каха заблудился в горах
00:57
К-Media
Рет қаралды 10 МЛН
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 117 МЛН
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 113 МЛН
How To Smooth Scroll in React - Smooth Scrolling Tutorial
8:10
Code Commerce
Рет қаралды 147 М.
Incredible scroll-based animations with CSS-only
32:23
Kevin Powell
Рет қаралды 403 М.
Stop, Intel’s Already Dead! - AMD Ryzen 9600X & 9700X Review
13:47
Linus Tech Tips
Рет қаралды 979 М.
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 531 М.
Scroll to Top of Page in React
8:12
Arslan
Рет қаралды 25 М.
2 Ways to Make a Scroll Parallax in React
16:54
Olivier Larose
Рет қаралды 13 М.
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 180 М.
You Don't Need a CMS - Use This Instead
28:55
Colby Fayock
Рет қаралды 18 М.
React Animations just got better
8:05
developedbyed
Рет қаралды 90 М.
#samsung #retrophone #nostalgia #x100
0:14
mobijunk
Рет қаралды 14 МЛН
Ускоряем ваш TV🚀
0:44
ARTEM_CHIBA
Рет қаралды 464 М.
📱магазин техники в 2014 vs 2024
0:41
djetics
Рет қаралды 706 М.
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 505 М.
Какой ноутбук взять для учёбы? #msi #rtx4090 #laptop #юмор #игровой #apple #shorts
0:18