I find myself so fascinated by parallax websites.
For those who do not know what that is, a parallax by definition is the effect where an object’s position and direction appear to differ from different positions, like through a viewfinder or camera lens. In the case of a parallax website, the position would be the position of the webpage when you scroll.
I first started working on a parallax project during my last module in my Flatiron School Bootcamp simply because I wanted to get my feet wet with it. I was so inspired by the websites on http://www.awwwards.com (definitely recommend checking out if you haven’t already! This is one of my favorites. )
It took awhile for me to start my project because I really had no idea of how to start one. So my developer instincts kicked in and began to Google.
However, I needed one specifically for a React project since building my project in React was one of the requirements.
I came across various libraries and different ways to incorporate a parallax into the project. Most of them were tutorials on how to make one using Vanilla Javascript and targeting specific elements on the HTML document. This doesn’t work in my case. I was running out of time so I decided to choose a library that was compatible with React. It was a special kind of parallax library that added a bounce effect when you finish scrolling through to a new section (see below).
Now that I’m not so restricted on time, I finally found the proper way of making one and wanted to share it! I found a video from Fullstack Academy, another prominent web development bootcamp, that shows an example of creating a parallax scrolling website. There were a couple of details that I needed to tweak to make it compatible with new ES6 syntax.
Let’s jump into it!
To reiterate, this is making a parallax component using React.js:
Starting the parallax component itself. This will be used to produce the different layers you will want on your website. With this component, you will be able to dictate the speed, direction, position, etc. elements of the your parallax layer.
This style object is defaulted as the components state. This will be where you can adjust the elements according to your taste through props or leaving it as is to its defaulted properties.
You might be wondering what those last two lines of code are for. These lines are for making sure that the parallax components are set and function properly once they are mounted. Since their states are dependent on how a user is scrolling, the method call this.handleScroll is used to set the “top” element accordingly.
The componentDidMount is what adds the event listener for scrolling. Once added, the event listener will be firing off A LOT more than we would like which would cause issues. To handle this, we have the throttle method which would fire off the event listener in a timely manner.
handleScroll is responsible for making sure the component knows what it’s top property should be set at every time the user scrolls. This will make sure that your parallax components appear and disappear from the page at the speed in which you would want it to.
And that’s it!
References:
Fullstack Parallax Scrolling Tutorial:
Parallax Springy: