visit
With
react-scroll
is a component to animate your vertical scroll, and it's effortless to use. In this article, I'm going to show you how to add it to your .After finishing this article, you're going to end up with this:git clone [email protected]:bernardogarza/react-scroll-tutorial.git
Make sure you're in the
react-smoothless
branch.git checkout react-smoothless
If you are working on my cloned repository, make sure to run
npm install
or yarn
(this might take a few minutes).If you are starting a project from scratch, you'll need to install the npm package.npm install react-scroll
// or
yarn add react-scroll
First things first, let's start the server, this will automatically refresh your browser when you save a file. You can view it in the browser at
localhost:3000
.npm start
//or
yarn start
The first thing we need to do to start using react-scroll is importing the
Link
component from react-scroll
into the component where we have our links, in this case, the Navbar
component.The Link component will allow us to animate our scrolling when we click on a link.// navbar.component.jsx
import React from 'react';
import styled from 'styled-components';
import { Link } from 'react-scroll';
As you can see, we are using styled-components but, that's a topic for a future article. Let's focus on the
Link
component for now.After we imported the Link component, we need to replace our
<a>
tags with the Link component.I recommend this extension for Visual Studio Code called , it automatically changes your closing tag while editing the opening one. It will save you a lot of time and confusion while editing tags.// navbar.component.jsx
export default function Navbar() {
return (
<NavbarStyled>
<div className="header">
<Link activeClass="active" to="section1" spy={true} smooth={true} duration={1000}>
Section 1
</Link>
<Link activeClass="active" to="section2" spy={true} smooth={true} duration={1000}>
Section 2
</Link>
<Link activeClass="active" to="section3" spy={true} smooth={true} duration={1000}>
Section 3
</Link>
</div>
<div className="scrolling-buttons">
<Wrapper>
<OnScreenScrolling />
</Wrapper>
</div>
</NavbarStyled>
);
}
You might be wondering what are all those properties inside the
Link
component, well, allow me to explain it to you:activeClass
: class applied when the element is reached. This means that whenever you click on the Link component, that class will be applied to that Link component.to
: target to scroll to. Here we are going to put the id of the element we want to scroll to.spy
: make Link selected when the scroll is at its target position.smooth
: animate the scrolling. That's basically the whole point of this.function (scrollDistanceInPx) { return duration; }
), that allows more granular control at run-time.Don't forget that the component that you want to link should have the same id that we used on the
to
property that we have in the Link
component. Here is an example:// section1.component.jsx
import React from 'react';
import styled from 'styled-components';
import Wrapper from '../wrapper/wrapper.component';
const Section1Styled = styled.div`
background-color: darkviolet;
.active {
border-bottom: 1px solid white;
}
`;
export default function Section1() {
return (
<Section1Styled id="section1">
<Wrapper>
<h1>Section 1</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat blanditiis adipisci eaque animi repellat
atque assumenda corporis quidem nostrum ea, nulla qui cupiditate suscipit, quisquam voluptas mollitia ex iusto
voluptates.
</p>
</Wrapper>
</Section1Styled>
);
}
You can see that we have
to="section1"
on our first Link
component.This is how our file should look://navbar.component.jsx
import React from 'react';
import styled from 'styled-components';
import { Link } from 'react-scroll';
import OnScreenScrolling from '../on-screen-scrolling/on-screen-scrolling.component';
import Wrapper from '../wrapper/wrapper.component';
const NavbarStyled = styled.div`
font-size: 24px;
position: fixed;
width: 100%;
.header {
background: black;
padding: 40px;
display: flex;
justify-content: space-around;
margin: 0;
color: white;
* {
cursor: pointer;
}
.active {
border-bottom: 1px solid white;
}
}
.scrolling-buttons {
display: flex;
flex-direction: column;
}
`;
export default function Navbar() {
return (
<NavbarStyled>
<div className="header">
<Link activeClass="active" to="section1" spy={true} smooth={true} duration={1000}>
Section 1
</Link>
<Link activeClass="active" to="section2" spy={true} smooth={true} duration={1000}>
Section 2
</Link>
<Link activeClass="active" to="section3" spy={true} smooth={true} duration={1000}>
Section 3
</Link>
</div>
<div className="scrolling-buttons">
<Wrapper>
<OnScreenScrolling />
</Wrapper>
</div>
</NavbarStyled>
);
}
And that's it, just like that you have
react-scroll
working on your Navbar links. Pretty easy, don't you think?
The
animateScroll
will allow us to perform more specific types of scrolling. We are not going to be limited to just scrolling straight to divs
with ids
. Using this we are going to be able to scroll from top to bottom and vice versa and scroll a specific amount of pixels per click.Let's start by importing the
animateScroll
into our on-screen-scrolling
file.// on-screen-scrolling.component.jsx
import React from 'react';
import styled from 'styled-components';
import { animateScroll as scroll } from 'react-scroll';
You can see that we are importing
animateScroll as scroll
, which is for replacing the name of the component to make it easier to write. You can give it the name you want. It would work the same way if we added this into our OnScreenScrolling
function: let scroll = animateScroll;
Now that we have imported
animateScroll
let's start working with the functions.To assign custom scrolling to our buttons, we will need to create functions that will be called using an
onClick handler
. The onClick handler
allows you to call a function and perform an action when an element is clicked.First, we need to create our functions, these need to be inside of the
OnScreenScrolling
function. These functions are pretty straight forward.Let's start with the functions that are going to scroll up.
The first one is going to be for scrolling to the top of our page.// on-screen-scrolling.component.jsx
function scrollToTop() {
scroll.scrollToTop();
}
// on-screen-scrolling.component.jsx
function scrollMoreUp300() {
scroll.scrollMore(-300);
}
// on-screen-scrolling.component.jsx
function scrollMoreUp100() {
scroll.scrollMore(-100);
}
Now, let's do the same but for down scrolling.
Let's start with the function to scroll to the bottom of the page. I bet you can guess how it is going to look like.// on-screen-scrolling.component.jsx
function scrollToBottom() {
scroll.scrollToBottom();
}
// on-screen-scrolling.component.jsx
function scrollMoreDown300() {
scroll.scrollMore(300);
}
// on-screen-scrolling.component.jsx
function scrollMoreDown100() {
scroll.scrollMore(100);
}
Now let put these functions to work. We will add them to our
<button>
tags using an onClick
handler, as I mentioned earlier.As you might already know, we need to use curly brackets to pass the functions inside our tag's properties, basic JSX syntax.// on-screen-scrolling.component.jsx
<OnScreenScrollingStyled>
<div className="top button-row">
<div>
<button onClick={scrollMoreUp100}>Scroll Up 100px</button>
</div>
<div>
<button onClick={scrollToTop}>Scroll to Top</button>
</div>
<div>
<button onClick={scrollMoreUp300}>Scroll Up 300px</button>
</div>
</div>
<div className="bottom button-row">
<div>
<button onClick={scrollMoreDown100}>Scroll Down 100px</button>
</div>
<div>
<button onClick={scrollToBottom}>Scroll to Bottom</button>
</div>
<div>
<button onClick={scrollMoreDown300}>Scroll Down 300px</button>
</div>
</div>
</OnScreenScrollingStyled>
There is not a lot to explain here, we just added the
onClick
handler inside each button and passed the function that we want them to call.This is how your file should look by now:import React from 'react';
import styled from 'styled-components';
import { animateScroll as scroll } from 'react-scroll';
const OnScreenScrollingStyled = styled.div`
height: 100vh;
display: flex;
flex-direction: column;
justify-content: space-between;
.button-row {
width: 100vw;
display: flex;
justify-content: space-between;
div {
margin: 0 10px;
}
button {
padding: 3px;
font-weight: bold;
cursor: pointer;
}
}
.bottom {
margin-bottom: 115px;
}
`;
export default function OnScreenScrolling() {
// Scroll up
function scrollToTop() {
scroll.scrollToTop();
}
function scrollMoreUp300() {
scroll.scrollMore(-300);
}
function scrollMoreUp100() {
scroll.scrollMore(-100);
}
// Scroll down
function scrollToBottom() {
scroll.scrollToBottom();
}
function scrollMoreDown300() {
scroll.scrollMore(300);
}
function scrollMoreDown100() {
scroll.scrollMore(100);
}
return (
<OnScreenScrollingStyled>
<div className="top button-row">
<div>
<button onClick={scrollMoreUp100}>Scroll Up 100px</button>
</div>
<div>
<button onClick={scrollToTop}>Scroll to Top</button>
</div>
<div>
<button onClick={scrollMoreUp300}>Scroll Up 300px</button>
</div>
</div>
<div className="bottom button-row">
<div>
<button onClick={scrollMoreDown100}>Scroll Down 100px</button>
</div>
<div>
<button onClick={scrollToBottom}>Scroll to Bottom</button>
</div>
<div>
<button onClick={scrollMoreDown300}>Scroll Down 300px</button>
</div>
</div>
</OnScreenScrollingStyled>
);
}
And that's it! We have fully functional scrolling buttons and links. The
animateScroll
is a little bit harder than Link
, but it's still straightforward and worth it.
“I guess I’ve been working so hard, I forgot what it’s like to be hardly working.” - Michael Scott
Catch me on - | |