Usability in web design, or how to evaluate your site from the point of view of the user
From the author: if you are visiting a site for the first time, then this event can be compared to traveling in an unfamiliar area: the local people, of course,…

Continue reading →

Usability in web design, or how to evaluate your site from the point of view of the user
From the author: if you are visiting a site for the first time, then this event can be compared to traveling in an unfamiliar area: the local people, of course,…

Continue reading →

Minimalistic website design: features, advantages and disadvantages
In the past few years, web design has alternately reigned in a flat style, now a skeuomorphism, now a material one, then again a flat one ... All of these…

Continue reading →

Productive Parallax

Do you like parallax or hate it, but it will not go anywhere. When used wisely, it can add depth and sophistication to a web application. The problem is that implementing parallax is productive – it’s not easy. In this article, we will discuss a solution that is both productive and, importantly, cross-browser.

TL; DR
Do not use scroll events or background-position to implement parallax.
For a better parallax effect, use CSS 3D transforms.
For Mobile Safari, use position: sticky to ensure that the parallax effect spreads.
If you need a turnkey solution, get Parallax helper JS from the UI Element Samples repository! You can watch the live demo.

Parallax problems
To begin, consider two general ways to implement the parallax effect, in particular, why they do not suit us.

Bad: using scroll events
The key requirement of parallax is that it must be associated with scrolling the page; each change in the scroll position must correspond to a change in the position of the elements to which parallax is applied. It sounds simple, but an important mechanism of modern browsers is the ability to work asynchronously. This applies, in our case, to the scroll event. In most browsers, scroll event handlers are executed on the principle of “best possible” (in the original, “best-effort”, approx. Translator) and do not guarantee animation with every change in scroll position!

This important information tells us why we should avoid a JavaScript-based solution that moves elements based on scroll events: JavaScript does not guarantee that the parallax will match the scroll position. In older versions of Mobile Safari, scroll events were actually triggered at the end of the scroll, which made it impossible to implement the parallax effect based on scroll events. In later versions, the animation is displayed while scrolling, but, like in Chrome, on the principle of “best possible.” If the main thread is busy with any other work, scroll events will not be executed immediately, which means that the parallax effect will be lost.

Bad: updating background-position
Another situation that we would like to avoid is rendering on each frame. Many solutions try to change the background position to provide a parallax effect, thereby causing the browser to redraw the affected parts of the page when scrolling. This solution can be expensive enough to slow down the animation significantly.

If we want to implement the parallax effect, we must apply the solution based on hardware accelerated properties (currently transform and opacity), which are independent of scroll events.

CSS in 3D
Both Scott Kellum and Keith Clark have done significant work in using CSS 3D to implement the parallax effect. The technique that they use effectively is as follows:

Set the container property overflow-y: scroll (and possibly overflow-x: hidden).
For the same element, apply the perspective value, and set the perspective-origin property to top left or 0 0.
For children, apply the Z-axis movement and scale them to provide a parallax effect without affecting their size on the screen.
The CSS for this looks like this:

CSS
.container {
width: 100%;
height: 100%;
overflow-x: hidden;
overflow-y: scroll;
perspective: 1px;
perspective-origin: 0 0;
}

.parallax-child {
transform-origin: 0 0;
transform: translateZ (-2px) scale (3);
}
These rules can be applied to a piece of code:

Markup
<div class = “container”>
<div class = “parallax-child”> </div>
</div>
Scale adjustment for perspective
Moving an element to the background makes it smaller in proportion to the perspective value. You can calculate how much you need to scale the element using the formula: (perspective – distance) / perspective. Since we want the element to be parallax but retain its original size, we must scale it this way.

In the case shown above, the perspective is 1px and the distance Z for parallax-child is −2px. This means that the item must be increased to 3x. This you can see in the code: scale (3).

For any element that does not have a translateZ value, you can replace the distance value with zero. This means that the scale will be (perspective – 0) / perspective, i.e. 1. This item does not need to be scaled up or down. Very comfortable, really.

How does this approach work?
It is important to understand why this approach works, as soon we will use this knowledge. Scrolling is actually a conversion, so it can be hardware accelerated; this is mainly due to moving layers using the GPU. In normal scrolling without perspective, scrolling occurs in a 1: 1 ratio if we compare the scrolling of the container and its children. If you scroll down an element 300px, the elements will also move 300px.

SOLID principles in JavaScript. Open-closed principle
The essence of the principle The principle of openness-closeness is as follows: "Software entities (classes, modules, functions, etc.) must be open for expansion, but closed for change." That is, the…

...

Flat Design Sites (Flat Design): history, principles, examples for inspiration
What do you think is the reason for such a frenzied popularity of flat design? I will answer you in a nutshell: it really works! In this article, I will…

...

Web Layout 101: How to Get the Most Out of the Top Right
From the author: studies of user behavior showed that the upper right corner of the site attracts the least attention. This is a common reflection of our reading habits. We…

...

Changing the site design: quick restart of your project
I wrote many times that the web design master must be creative, must look for new solutions to everyday situations, keep an eye on fashion ... But unfortunately, fashion is…

...