How to scroll to a component when the user clicks on any link or button

Hello, Guys I have a homepage where there are some components and each component is having full-screen height. let's say in the hero component on the homepage there is a navbar component where I have some links. What I want is that whenever the user clicks on any link it should scroll to the component associated with it.
39 Replies
StefanH
StefanH2y ago
Add an id to each element you want to navigate to. You can then link to those elements using the fragment part of the url (example.com/home#contact) To enable scrolling use scroll-behavior: smooth;
Hmm
Hmm2y ago
use intersection observer api or use react-scroll library https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
Intersection Observer API - Web APIs | MDN
The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.
Hmm
Hmm2y ago
you can spy on the link whenever they are under viewport and change the style
StefanH
StefanH2y ago
You don't need JS for that
Hmm
Hmm2y ago
yes except when you also want to style the link whenver user scroll to it
StefanH
StefanH2y ago
The example on the scroll-behavior should cover all you need https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior#examples That wasn't the question. Also if you want to style the element which got navigated to you can use the :target pseudo class https://developer.mozilla.org/en-US/docs/Web/CSS/:target
Hmm
Hmm2y ago
oh yeah idk that , I used intersection api in my project this one is better
nehatkhan
nehatkhanOP2y ago
I tried the scroll-library but it throw error saying didn't find the target id which is true as the component is not in the dom initially when I scroll down that only it comes to dom. I am bit new with react I do not want to style just need the scrolling behavior
StefanH
StefanH2y ago
If you only want scrolling you just need scroll-behavior: smooth;. Check out the example i sent you
nehatkhan
nehatkhanOP2y ago
no sir, I mean it should scroll when user clicks on link
StefanH
StefanH2y ago
Exactly that's what that does
nehatkhan
nehatkhanOP2y ago
wait I will show the code of the navbar with scroll library
nehatkhan
nehatkhanOP2y ago
I use link component in navbar of react scroll and assign the id
nehatkhan
nehatkhanOP2y ago
and put the same id to the component like this <Who id='who' />
nehatkhan
nehatkhanOP2y ago
this is the thing I get
StefanH
StefanH2y ago
You don't need react or js to do this. If you don't look at the multiple docs pages and examples I sent you, I can't help you
nehatkhan
nehatkhanOP2y ago
I am looking only as per docs I have to provide hreff to the link right and id to the component I am using material ui typography component <nav> <a href="#page-1">1</a> <a href="#page-2">2</a> <a href="#page-3">3</a> </nav> <div class="scroll-container"> <div class="scroll-page" id="page-1">1</div> <div class="scroll-page" id="page-2">2</div> <div class="scroll-page" id="page-3">3</div> </div> this example is taking me to the different page which doesnt exists I tried this way
nehatkhan
nehatkhanOP2y ago
nehatkhan
nehatkhanOP2y ago
StefanH
StefanH2y ago
Are you using react to make a single page application?
nehatkhan
nehatkhanOP2y ago
Yes I am using react sir Its for my portfolio https://nehat-dev.vercel.app Check this sir When I scroll up its scrolling 100% screen height.
StefanH
StefanH2y ago
I can imagine that this'll cause issues on SPAs as a link would move you from the current page. Didn't know you're making a spa
WebMechanic
WebMechanic2y ago
@nehat.dev if a simple #page-x takes you to another page, then the React Router is configured to use the (legacy) "hash history". Modify the setting so it uses "browser history" https://v5.reactrouter.com/web/api/history
ReactRouterWebsite
React Router: Declarative Routing for React
Learn once, Route Anywhere
Electronic
Electronic2y ago
In your highest priority component wrap it in a div or smtng with an id Then put a tags with those IDs in your navbar component and add scroll-beheviour smooth
nehatkhan
nehatkhanOP2y ago
I am not using any routing just a single-page application sir Do you mean I should then wrap the ‘who’ component inside a div and assign id to that div? But the component didn't render initially, its only render when the user scrollss down. That how come it get the id when there is no data mounted Yes sir.
Hmm
Hmm2y ago
If talking about myself , I used Intersection Observer API for my portfolio . @nehat.dev Do you want to achieve the smooth scroll on single page?
nehatkhan
nehatkhanOP2y ago
Yes sir right
WebMechanic
WebMechanic2y ago
If users can "navigate" from one state of the page to another and back, showing different contents, that's part of routing, too. it does not imply or mean to navigate to another "physical" page, which is why this is called "Single page application" but to represent a specific set of contents (like pages) via their unique URL so users and search engines can get there via bookmarks etc. and have a consistent experience.
nehatkhan
nehatkhanOP2y ago
Hmm
Hmm2y ago
I am not gonna give you the answer right away but read through https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API All You have to do is observer your section with .isIntersecting Like I do in https://tjsm.netlify.app/
nehatkhan
nehatkhanOP2y ago
Yes sir make sense sorry I didn’t understand nicely
Hmm
Hmm2y ago
Make the footer sticky so you can see the scrolling highlight
nehatkhan
nehatkhanOP2y ago
Thank you sir. Will check and try to implement I have not created yet sir, still building I want exactly the same thing sir what you used after googling I found that css snap type is causing the issue with react scroll libraryr. but didnt get the solution. But I manage to use scroll on top method const ScrollToTop = ()=>{ wrapperRef.current.scrollTo({ top: 0, behavior: "smooth", }); } with this code @hmm_69 this wrapper referrance i put to the main container where i use snap type
Hmm
Hmm2y ago
there is no need of useRef hook and other stuff , just subscibie the section to observer api
nehatkhan
nehatkhanOP2y ago
Can you please share any article or guide or any video to get some ides
Hmm
Hmm2y ago
Web Dev Simplified
YouTube
Learn Intersection Observer In 15 Minutes
Intersection Observer is an amazing tool in JavaScript that allows you to easily and efficiently do pretty much anything based on the visibility of elements. This is perfect for scroll animations, lazy loading images, infinite scroll, and much more. Also, Intersection Observer is incredibly performant and flexible which makes it by far the best ...
Hmm
Hmm2y ago
GitHub
dev_portfolio/Navitems.tsx at main · Tribhuwan-Joshi/dev_portfolio
My personal portfolio . Contribute to Tribhuwan-Joshi/dev_portfolio development by creating an account on GitHub.
Hmm
Hmm2y ago
or see how I implement it
Want results from more Discord servers?
Add your server