Quick Tip: Create a Very Simple Parallax Effect With CSS & JavaScript

In this quick tip tutorial we’ll learn to add a simple yet useful parallax scrolling effect to a hero image. To achieve it we’ll use a bit of JavaScript code, but keep in mind that you can create pure CSS parallax websites by combining 3D transforms.

Here’s the page that we're going to build. Scroll down!

Let’s build it in three fundamental steps: beginning with the markup, then the styling, then the behavioral JavaScript.

1. Begin With the HTML Markup

The page markup consists of two sections. The first section holds the hero image and its child elements, while the second section contains the page’s main content:

Notice the data-direction and data-speed custom attributes assigned to the elements of the first section. These elements are parallax elements and the values of their attributes will determine their behavior during the parallax scrolling. More on that in a minute!

2. Add the CSS

Next we’ll specify a few CSS rules for our page. Nothing really important, just some basic stuff that will enhance the page layout:

3. Apply the JavaScript

To create the parallax effect, we’ll listen to the scroll event.

Each time the page is scrolled, we loop through the parallax elements and do the following things:

  • We retrieve the value of their data-speed attribute and multiply it by the number of pixels that the document is scrolled vertically. That gives us the speed at which each of the parallax elements runs. So for example, data-speed = 1 means that an element will move as quickly as the page content. On the other hand, an element with data-speed = 0.5 will move 50% slower than the page scrolling.
  • We retrieve the value of their data-direction attribute. This attribute specifies where the parallax elements will move as the page gets scrolled. Possible values are up and down. So for example, data-direction = "up" means that an element will move upwards. In our case, everything but the image will move in an upward direction.
  • Based on their attribute values, we use the translate3d() function to move the elements either upwards or downwards. 

Here’s the corresponding JavaScript code:

Conclusion

That’s it! In this quick tip, we managed to create a really simple parallax scrolling effect with just a few lines of JavaScript. Again, as already discussed, this is a basic demo and might not be suitable for all parallax cases. 

Parallax effects can certainly add character to your website, but the browser may have to perform “expensive” tasks to build them, using up resources and impeding performance. Be sure to take performance into account during your parallax implementations.

However, if you want to create a more advanced parallax experience, you can have a look at some JavaScript libraries which exist out there like parallax.js.

As always, thanks for reading!

More Parallax Goodness on Tuts+

  • Web Design
    What Is Parallax Scrolling?
    Craig Campbell
  • WordPress
    How to Make a Simple One Page Website: From a Parallax WordPress Theme
    Brenda Barron
  • Inspiration
    Web Design Inspiration: Scrollin’, Scrollin’, Scrollin’
    Ian Yates




from Envato Tuts+ Tutorials

Comments

Popular posts from this blog

How to Build Self-Confidence: 10+ Steps to Improve Your Self-Esteem

Best of 2020: Great Landing Pages for Design Inspiration (Showcase Examples)

18+ Best Free PowerPoint Puzzle Pieces Templates (Infographic PPT Slides)