background-attachment

如果指定了 background-image ,那么 background-attachment 决定背景是在视口中固定的还是随包含它的区块滚动的。

 

语法

background-attachment: fixed;
background-attachment: local;
background-attachment: scroll;

描述
fixed此关键字表示背景相对于视口固定。即使一个元素拥有滚动机制,背景也不会随着元素的内容滚动。
local此关键字表示背景相对于元素的内容固定。如果一个元素拥有滚动机制,背景将会随着元素的内容滚动, 并且背景的绘制区域和定位区域是相对于可滚动的区域而不是包含他们的边框。
scroll此关键字表示背景相对于元素本身固定, 而不是随着它的内容滚动(对元素边框是有效的)。

例子

HTML

<p>
  There were doors all round the hall, but they were all locked; and whenAlice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again.
</p>

CSS

p {
  background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif");
  background-attachment: fixed;
}

执行结果

There were doors all round the hall, but they were all locked; and when Alice had been all the way down one side and up the other, trying every door, she walked sadly down the middle, wondering how she was ever to get out again.

参考