Style backgroundAttachment 属性

定义和用法

backgroundAttachment 属性设置或返回背景图像是应随内容滚动还是固定的。

另请参阅:

HTML 样式:background 属性

CSS 教程:CSS 背景

CSS 参考手册:background-attachment 属性

实例

例子 1

把背景图像设置为固定(不会滚动):

document.body.style.backgroundAttachment = "fixed";

亲自试一试

例子 2

在 DIV 元素上选择 scroll 和 local:

document.getElementById("myDIV").style.backgroundAttachment = "local";

亲自试一试

例子 3

在 scroll 和 fixed 之间切换:

var x = document.body.style.backgroundAttachment;
document.body.style.backgroundAttachment = (x == "scroll")? "fixed":"scroll";

亲自试一试

例子 4

返回 background-attachment 属性的值:

alert(document.body.style.backgroundAttachment);

亲自试一试

语法

返回 backgroundAttachment 属性:

object.style.backgroundAttachment

设置 backgroundAttachment 属性:

object.style.backgroundAttachment = "scroll|fixed|local|initial|inherit"

属性值

描述
scroll背景随着元素滚动。默认。
fixed背景相对于视口是固定的。
local背景随着元素的内容滚动。
initial将此属性设置为其默认值。请参阅 initial
inherit从其父元素继承此属性。请参阅 inherit

技术细节

默认值:scroll
返回值:字符串,表示背景图像如何附加到文档中的对象。
CSS 版本:CSS1

浏览器支持

backgroundAttachment 是 CSS1 (1996) 特性。

所有浏览器都完全支持它:

ChromeEdgeFirefoxSafariOperaIE
ChromeEdgeFirefoxSafariOperaIE
支持支持支持支持支持支持