I had a case when I needed to refactor code in my work. More precisely, I had to remove unnecessary libraries. One of these libraries created parallax effects when scrolling the page.

This library was used on many pages and in different ways. But it was poorly adaptive and complex to maintain or extend. For example, it didn't support 3d transformations, and for the only element on the site that required it, another library was installed.

Therefore, it was decided to write its directive, replacing both libraries and removing two dependencies at once. With the ability to extend the functionality and use in Vue 3.

Image.

Implementing the default solution

The basic functionality, which is enabled by default without options, is to work with two card columns, with several of them inside. The important point is that one of the columns must be greater in height than the one next. 

For example, here:

Parallax effect.

You need to replicate a similar layout using FlexBox or Grid CSS. And set align-seft: flex-start to the column, which should be adjusted to the height of the next column. This is necessary to correctly calculate the pixels by which the column should move while scrolling.

In this case, the layout looks like this:

When using the default directive, you just need to import the mad-parallax.js file into the component and use it on the column you want to move.

Image.

Implementing a solution with custom options

Default solutions can make life a lot easier, but they may not be suitable in some cases. So, in addition to the default solution, you can specify custom parameters for element shifts when scrolling. 

For example, here:

Parallax effect with options.

To get this effect, you need to set the desired options for the cards to move. For example, we have to move the second and third cards. We need to set the following options:

In the first card, we turn off the parallax. Obviously, the units within the parameters are pixels. For the second card, we set maxMove: 20. For the third card, maxMove: 40. Thus the cards will appear one after the other while scrolling.

Component source code:

Summary

The nice thing about using a directive with options is that you can animate any element on the page: cards, images, or even a header specifying the shift you want. Also, you can extend the directive with functions you need or move to Vue 3 easily.

You can look at the implementation here and learn the source code and documentation for the directive in detail on GitHub.

Image

Latest articles here

Introduction to the World of Application Design.

Software Architecture 101 – Introduction to the World of Application Design

Hello, everyone!Today, I would like to introduce you to the world of application design.Designing applications can be called a multidisciplinary...

EmberJS and ReactJS for Web Development.

EmberJS and ReactJS for Web Development

Selecting the right frontend framework is key to ensuring a smooth user experience and achieving overall project success.React JS and Ember JS are...

Everything you need to quick-start a new React project inside one repo.

React Boilerplate: Everything You Need to Quick-Start a New React Project Inside One Repo

When creating a new React project, I faced the same problems every time. I need to build it manually because <code...

Go to blog