body{ background: url("../images/bg.jpg") no-repeat center center / cover fixed; }
cover
property, but the point was not in it. Turning off the fixed background position (removing the fixed one), my Crysis gave me more than 30 FPS ! "In business ...", I thought. How so? Why? Why did I not notice this before? Perhaps this is not very noticeable on lightweight sites where there are not so many html elements.background-attachment : fixed
each time it scrolls it causes a redraw operation. The page must move its content. And when it comes to a fixed background, the browser must re-draw the image in a new location, relative to existing DOM elements.will-change
property. About him will be discussed below. body{ background: url("../images/bg.jpg") no-repeat center center; background-attachment: fixed; background-size: cover; }
body{ position: relative; } body::before { background: url("../images/bg.jpg") no-repeat center center; background-size: cover; content: ' '; height: 100%; left: 0; position: fixed; top: 0; width: 100%; will-change: transform; z-index: -1; }
position: relative
to the body
element to then position the pseudo-element, which will be a separate layer for our background. The rest of the properties regarding the background, we moved to ::before
. In the pseudo-element, we now use position : fixed
, instead of the previous background-attachment: fixed
in body
. And most importantly, without which the whole undertaking will fail, this property will-change .will-change
property instructs the browser to display an element, regardless of other elements surrounding it. It seems to be saying to the browser: “Hey, friend, this element will change sometime later, in the future, so draw it only once on its own layer. And do not take into account the other elements - it is in itself. "Source: https://habr.com/ru/post/282079/
All Articles