Code

JavaScript Parallax Effect for Your Website |

JavaScript Parallax Effect for Your Website | Skillbox Media

Dive into the profession of a front-end developer with an emphasis on AI

Find out more

Parallax is an optical illusion that creates the impression of an object moving against the background when the observer changes their position. This effect can be easily demonstrated by holding your finger to your face and looking at it first with one eye and then with the other.

Source

In this article, we will discuss the process of developing an animated background and other components for a website using JavaScript.

The source code is available on the GitHub platform.

Parallax Background: A Play of Depth and Movement

First, let's create the two necessary images—one for the foreground and one for the background. I chose a photo of space as the background:

I cut out a simple landscape from another photo to use in the foreground.

Now we need to create layouts for the components that will display the images.

Alternatively, you can set one background for the body element and another for the wrapper. Update the code to suit your project's requirements. Next, let's add the styles:

The elements have a fixed placement on the page, where one element, representing a tree, is on top of another, which depicts space. We also applied a blur effect to the tree, which helps hide its unevenness and creates the impression of being out of focus.

You need to create a script:

Let's pay attention to the result:

There is no limit to the number of layers you can use. However, keep in mind that layers located further away should move more slowly. For example, if the viewer takes a few steps, this will not affect the position of the stars. On the contrary, a tree located closer to the viewer will move at a greater speed.

The Parallax Effect in the Particle World

The parallax effect is applicable not only to background images, but also to elements located in the foreground. For example, similar techniques are often found in the design of Apple landing pages.

Source

The page will integrate elements that will be located on top of the main content and will move in accordance with the scrolling.

First, you need to create The container in which the particles will be placed.

The block isn't visible to the user—it serves as a container. Now let's set up the styles:

The particle properties are defined using a script.

Now you can see how it works:

Certainly, there is significant room for improvement in this direction. You can experiment with the size and shape of the particles at different levels, as well as work on the perspective and blur effect. The optimal parameters can be identified only through trial and error.

The fusion of front-end development and artificial intelligence

Thanks to the efforts of front-end development specialists, we are able to like, leave comments, add products to cart and easily navigate online maps. This course will teach you how to create user interfaces for web services using various programming languages ​​and additional tools. You will be able to develop applications such as task schedulers, instant messengers, and online stores.

Find out more