React
HTML
<div id="app"></div>
React
function App() {
let ref1 = React.useRef();
let ref2 = React.useRef();
function scrollTo(ref) {
if (!ref.current) return;
ref.current.scrollIntoView({ behavior: "smooth" });
}
return (
<div>
<button onClick={() => scrollTo(ref1)}>First element anchor</button>
<button onClick={() => scrollTo(ref2)}>Second element anchor</button>
<div style={{ height: "800px" }}>Just a spacer</div>
<div style={{ height: "800px" }} ref={ref1}>
Element 1
</div>
<div style={{ height: "800px" }} ref={ref2}>
Element 2
</div>
</div>
);
}
ReactDOM.render(<App />, document.querySelector("#app"));