Set a background-image to be fixed (will not scroll):
document.body.style.backgroundAttachment = "fixed";
Try it Yourself »
More "Try it Yourself" examples below.
The backgroundAttachment property sets or returns whether a background image should scroll with the content, or be fixed.
HTML Styles: The background Property
CSS Tutorial: CSS Backgrounds
CSS Reference: The CSS background-attachment Property
Return the backgroundAttachment property:
object.style.backgroundAttachment
Set the backgroundAttachment property:
object.style.backgroundAttachment = "scroll|fixed|local|initial|inherit"
Value | Description |
---|---|
scroll | The background scrolls along with the element. This is default |
fixed | The background is fixed with regard to the viewport |
local | The background scrolls along 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 |
Default Value: | scroll |
---|---|
Return Value: | A String, representing how the background image is attached to the object within the document |
CSS Version | CSS1 |
backgroundAttachment
is a CSS1 (1996) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
Choose between scroll and local on a DIV element:
document.getElementById("myDIV").style.backgroundAttachment = "local";
Try it Yourself »
Toggle between scroll and fixed:
let x = document.body.style.backgroundAttachment;
document.body.style.backgroundAttachment = (x == "scroll")? "fixed":"scroll";
Try it Yourself »
Return the value of the background-attachment property:
let back = document.body.style.backgroundAttachment;
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!