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
How to Configure SSG and SSR on Nuxt.

How to Configure SSG and SSR on Nuxt

When I started learning Vue, I was pleasantly surprised by this framework. It turned out to be very simple and easy to learn. Its API had enough...

How to configure Apollo GraphQL in Nuxt application.

How to Configure Apollo GraphQL in Nuxt Application

Good time of the day, friends!Today I want to show you how to set up Apollo GraphQl in a Nuxt application, where nuxt will take care of both client...

Strapi - Things to Keep in Mind Before Migrating to This CMS

Strapi: Things to Keep in Mind Before Migrating to This CMS

When it comes to CMS, the modern market has already stepped so much that the choice is becoming increasingly difficult. There are so many options for...