Created
February 27, 2024 14:45
-
-
Save pebueno/31ddf55295685181d2f89a2d23a981a2 to your computer and use it in GitHub Desktop.
Scroll To Section with Ref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Task is to implement scroll to section for buttons in the menu without anchoring | |
const Component = () => { | |
const section2Ref = React.useRef(null); | |
const section3Ref = React.useRef(null); | |
const section4Ref = React.useRef(null); | |
const scrollToSection = (sectionRef) => { | |
if (sectionRef.current) { | |
sectionRef.current.scrollIntoView({ behavior: "smooth" }); | |
} | |
}; | |
return ( | |
<div className="container"> | |
<div className="menu"> | |
<a href="#section1">Section 1</a> | |
<button onClick={() => scrollToSection(section2Ref)}>Section 2</button> | |
<button onClick={() => scrollToSection(section3Ref)}>Section 3</button> | |
<button onClick={() => scrollToSection(section4Ref)}>Section 4</button> | |
</div> | |
<h1>Page Title</h1> | |
<h2 id="section1">Section 1</h2> | |
<p> | |
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. | |
</p> | |
<h2 ref={section2Ref}>Section 2</h2> | |
<p> | |
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. | |
</p> | |
<h2 ref={section3Ref}>Section 3</h2> | |
<p> | |
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. | |
</p> | |
<h2 ref={section4Ref}>Section 4</h2> | |
<p> | |
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. | |
</p> | |
</div> | |
) | |
} | |
ReactDOM.render(<Component />, document.querySelector("#app")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.container { | |
width: 300px; | |
} | |
h2 { | |
margin-top: 16px; | |
font-weight: 700; | |
font-size: 22px; | |
} | |
h1 { | |
margin-bottom: 20px; | |
font-size: 32px; | |
font-weight: 700; | |
} | |
.menu { | |
position: fixed; | |
left: 350px; | |
top: 20px; | |
border-radius: 6px; | |
border: 2px solid green; | |
padding: 16px; | |
display: flex; | |
flex-direction: column; | |
gap: 16px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment