Low FPS when scrolling the page. Solving the background-attachment: fixed problem

By default, when you scroll a web page with a background, the background scrolls along with other elements. Sometimes this is appropriate, and sometimes you want the background-image to always be visible. There is a background-attachment property to manage this state.

Background-attachment values

The property can take three values ​​to customize the background behavior:

  • scroll (default) - The background image moves with other elements when the web page is scrolled.
  • fixed - the value fixes the background image, making it insensitive to scrolling. As you scroll down the page, the background in the background will remain stationary.
  • local - this value was added due to the fact that in the case of local scrolling, the background with the value scroll Behave like fixed... When the value local the background scrolls with the element's content, and does not scroll unless the content scrolls (but scrolls with the element itself).

For a better understanding of how the property works, take a look at the examples below. So that you can compare them, your all three background-attachment values ​​(hopefully you're using a modern desktop browser when learning CSS!).

background-attachment: scroll

background-attachment: fixed

background-attachment: local

Browser support

Desktop browsers IE9 +, Edge 12+, Firefox 25+, Chrome (all versions), Safari 5+, Opera 11.5+ have full support for all values ​​of the background-attachment property. From mobile browsers Opera Mobile, Firefox for Android, IE Mobile and QQ Browser understand all the meanings. In other browsers, support is either partial or absent.

Further down the tutorial: background-origin and background-clip properties for positioning background images and controlling cropping.

This little lesson will be about how to make a beautiful fixed background on your website (or on a one-page page) that will smoothly flow from one image to another when the user scrolls the page. CSS has a background-attachment: fixed property that allows the background image to be fixed. That is, when scrolling the page, it will not scroll with the page. But there is no CSS property to make the images change when scrolling, so we'll use Javascript.

Fixed background for the site

Perhaps you do not understand what I am writing about, so watch the demo and you yourself will see this beautiful effect:

Download

How to make a changing background for a website?

Html

We'll start by defining the HTML structure of the page for which we are doing this effect:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 <div id = "cbp-fbscroller" class = "cbp-fbscroller">

In the tag