14
loading...
This website collects cookies to deliver better user experience
ref
to store the reference of the element that you want to scroll to. And call myRef.current.scrollIntoView()
to scroll it into the view.import React, { useRef } from 'react';
const App = () => {
const scollToRef = useRef();
return (
<div className="container">
<button onClick={() => scollToRef.current.scrollIntoView()}>
Scroll
</button>
<div ref={scollToRef}>You scrolled to me</div>
</div>
);
};
export default App;
import React, { createRef } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.scollToRef = createRef();
}
render() {
return (
<div className="container">
<button onClick={() => this.scollToRef.current.scrollIntoView()}>
Scroll
</button>
<div ref={this.scollToRef}>You scrolled to me</div>
</div>
);
}
}
export default App;
scrollTo
function that lets you scroll to a given location. A good application for this function is to scroll to the bottom of the page, or back to the top. This function takes two arguments: the first is the position of where you want to scroll and the second is the animation duration (optional). The syntax for this function is as follows: scrollTo(x, y)
.scrollTo
function can be accessed directly from Window
object.import React from "react";
const App = () => {
return (
<div className="container">
<button onClick={() => window.scrollTo(0, window.innerHeight)}>
Scroll to bottom
</button>
<button onClick={() => window.scrollTo(0, 0)}>Scroll top top</button>
</div>
);
};
export default App;
scrollTo
function with ScrollToOptions
object as a parameter. I'm using this when I need to specify additional scrolling behavior, e.g. smooth scrolling.import React from "react";
const App = () => {
return (
<div className="container">
<button
onClick={() =>
window.scrollTo({
left: 0,
top: window.innerHeight,
behavior: "smooth",
})
}
>
Scroll to bottom
</button>
<button
onClick={() =>
window.scrollTo({
left: 0,
top: 0,
behavior: "smooth",
})
}
>
Scroll top top
</button>
</div>
);
};
export default App;
import React, { Component } from "react";
import {
Link,
Element,
Events,
animateScroll as scroll,
scroller,
} from "react-scroll";
import "./ReactScroll.css";
class ReactScroll extends Component {
componentDidMount() {
Events.scrollEvent.register("begin", () => {
console.log("begin", arguments);
});
Events.scrollEvent.register("end", () => {
console.log("end", arguments);
});
}
scrollToTop = () => {
scroll.scrollToTop();
};
scrollTo = () => {
scroller.scrollTo("scroll-to-element", {
duration: 800,
delay: 0,
smooth: "easeInOutQuart",
});
};
scrollToWithContainer = () => {
let goToContainer = new Promise((resolve) => {
Events.scrollEvent.register("end", () => {
resolve();
Events.scrollEvent.remove("end");
});
scroller.scrollTo("scroll-container", {
duration: 800,
delay: 0,
smooth: "easeInOutQuart",
});
});
goToContainer.then(() =>
scroller.scrollTo("scroll-container-second-element", {
duration: 800,
delay: 0,
smooth: "easeInOutQuart",
containerId: "scroll-container",
})
);
};
componentWillUnmount() {
Events.scrollEvent.remove("begin");
Events.scrollEvent.remove("end");
}
render() {
return (
<main className="react-scroll-page">
<nav>
<ul>
<li>
<Link to="section-1" spy={true} smooth={true}>
Section 1
</Link>
</li>
<li>
<Link to="section-2" spy={true} smooth={true}>
Section 2
</Link>
</li>
<li>
<Link to="section-3" spy={true} smooth={true}>
Section 3
</Link>
</li>
<li>
<button onClick={scroll.scrollToBottom}>Scroll To Bottom</button>
</li>
<li>
<button onClick={scroll.scrollToTop}>Scroll To Top</button>
</li>
<li>
<button onClick={() => scroll.scrollMore(500)}>
Scroll 500 More!
</button>
</li>
<li>
<button onClick={() => scroll.scrollMore(-500)}>
Scroll 500 Less!
</button>
</li>
<li>
<button
to="scroll-container-second"
onClick={() => this.scrollToWithContainer()}
>
Scroll to second element within container
</button>
</li>
</ul>
</nav>
<Element name="section-1" className="page-section page-section-1">
Section 1
</Element>
<Element name="section-2" className="page-section page-section-2">
Section 2
</Element>
<Element name="section-3" className="page-section page-section-3">
Section 3
</Element>
<Element className="container" id="scroll-container">
<Element name="scroll-container-first">
First element inside container
</Element>
<Element name="scroll-container-second">
Second element inside container
</Element>
<Element name="scroll-container-third">
Third element inside container
</Element>
</Element>
</main>
);
}
}
export default ReactScroll;
import React from "react";
import { Link } from "react-router-dom";
import { HashScroll } from "react-hash-scroll";
const HashScrollPage = () => {
return (
<main className="page">
<nav>
<ul>
<li>
<Link to="/hash-scroll#hash-section-1">Go To Section 1</Link>
</li>
<li>
<Link to="/hash-scroll#hash-section-2">Go To Section 2</Link>
</li>
<li>
<Link to="/hash-scroll#hash-section-3">Go To Section 3</Link>
</li>
</ul>
</nav>
<article>
<HashScroll hash="#hash-section-1">
<section>Section 1</section>
</HashScroll>
<HashScroll hash="#hash-section-2">
<section>Section 2</section>
</HashScroll>
<HashScroll hash="#hash-section-3">
<section>Section 3</section>
</HashScroll>
</article>
</main>
);
};
export default HashScrollPage;