CSS - свойство background-attachment
Пример
Фоновое изображение, которое не будет прокручиваться вместе со страницей (исправлено):
body {
background-image: url("img_tree.gif");
background-repeat: no-repeat;
background-attachment: fixed;
}
Другие примеры «Попробуйте сами» ниже.
Определение и использование
Свойство background-attachment
устанавливает, будет ли фоновое изображение прокручиваться вместе с остальной частью страницы или будет фиксированным.
Значение по умолчанию: | прокрутить |
---|---|
Унаследовано: | нет |
Анимация: | нет. Читать об анимации |
Версия: | CSS1 |
Синтаксис JavaScript: | объект .style.backgroundAttachment="фиксированный" |
Поддержка браузера
Цифры в таблице указывают на первую версию браузера, которая полностью поддерживает это свойство.
Property | |||||
---|---|---|---|---|---|
background-attachment | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Синтаксис CSS
background-attachment: scroll|fixed|local|initial|inherit;
Значения свойств
Value | Description |
---|---|
scroll | The background image will scroll with the page. This is default |
fixed | The background image will not scroll with the page |
local | The background image will scroll with the element's contents |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Дополнительные примеры
Пример
Фоновое изображение, которое будет прокручиваться вместе со страницей (прокрутка). Это по умолчанию:
body {
background-image: url("img_tree.gif");
background-repeat: no-repeat;
background-attachment: scroll;
}
Пример
Как создать простой эффект прокрутки параллакса (создать иллюзию трехмерной глубины):
.fixed-bg {
/* The background image */
background-image: url("img_tree.gif");
/* Set a specified height, or the minimum height for the background image */
min-height: 500px;
/* Set background image to fixed (don't scroll along with the page) */
background-attachment: fixed;
/* Center the background image */
background-position: center;
/* Set the background image to no repeat */
background-repeat: no-repeat;
/* Scale the background image to be as large as possible */
background-size: cover;
}
Связанные страницы
Учебное пособие по CSS: Фон CSS
Справочник по HTML DOM: свойство backgroundAttachment