樣式 backgroundAttachment 屬性
更多“自己嘗試”的例子見下文。
描述
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 |
是 | 是 | 是 | 是 | 是 | 是 |
更多示例
示例
在 DIV 元素上選擇 scroll 和 local
document.getElementById("myDIV").style.backgroundAttachment = "local";
自己動手試一試 »
示例
在 scroll 和 fixed 之間切換
let x = document.body.style.backgroundAttachment;
document.body.style.backgroundAttachment = (x == "scroll")? "fixed":"scroll";
自己動手試一試 »