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) 特性。

所有瀏覽器都完全支持它:

Chrome Edge Firefox Safari Opera IE
Chrome Edge Firefox Safari Opera IE
支持 支持 支持 支持 支持 支持