requestAnimationFrame . It turns out a qualitatively different result, but why not get rid of JavaScript at all? <div class="parallax"> <div class="parallax__group"> <div class="parallax__layer parallax__layer--back"> ... </div> <div class="parallax__layer parallax__layer--base"> ... </div> </div> <div class="parallax__group"> ... </div> </div> .parallax { perspective: 1px; height: 100vh; overflow-x: hidden; overflow-y: auto; } .parallax__layer { position: absolute; top: 0; right: 0; bottom: 0; left: 0; } .parallax__layer--base { transform: translateZ(0); } .parallax__layer--back { transform: translateZ(-1px); } parallax class. Defining the properties of the height and perspective styles will set the perspective of the element at its center, creating a fixed 3D viewport. overflow-y: auto will allow the content inside the element to scroll normally, while the descendants of the element will be drawn relative to a fixed perspective. This is the key to creating a parallax effect.parallax__layer class. As the name implies, it defines the content layer to which the parallax effect will be applied. An element with this class is torn out of the general flow of content and positioned to fill its container.parallax__layer--base and parallax__layer--back modifier classes. They are needed to adjust the speed of scrolling the parallax of elements, shifting them along the Z axis (removing or approaching the viewport). For brevity, I made only two scrolling speeds - later we will add a few more.scale() transformation so that the element is drawn in its original size: .parallax__layer--back { transform: translateZ(-1px) scale(2); } 1 + (translateZ * -1) / perspective) . For example, if the perspective of the viewport is set as 1px and we shift the element by -2px along the Z axis, then the coefficient will be scale(3) . .parallax__layer--deep { transform: translateZ(-2px) scale(3); } translateZ(-10px) will scroll slower than translateZ(-1px) ).parallax__group element to group our layers together: <div class="parallax"> <div class="parallax__group"> <div class="parallax__layer parallax__layer--back"> ... </div> <div class="parallax__layer parallax__layer--base"> ... </div> </div> <div class="parallax__group"> ... </div> </div> .parallax__group { position: relative; height: 100vh; transform-style: preserve-3d; } height: 100vh , although, if necessary, the number for each group may be different. transform-style: preserve-3d does not allow the browser to flatten elements with parallax__layer , and position: relative allows child parallax__layer elements to be positioned relative to their group.overflow: hidden element of the parallax__group element will break the entire parallax effect. Uncircumcised content will cause child elements to go beyond. Therefore, you need to poshamanit with the group's z-index value in order to be sure that the content will be correctly hidden and displayed as the user scrolls the site. .parallax__group { transform: translate3d(700px, 0, -800px) rotateY(30deg); } debug !preserve-3d (on the way), so parallax will not work. But this is normal, because you still need to design it so that the content is adequate and without parallax - Well, progressive enhancement and all that!Source: https://habr.com/ru/post/235531/
All Articles