ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# CSS background-attachment 属性 ## 实例 如何设置固定的背景图像: ``` body { background-image: url(bgimage.gif); background-attachment: fixed; } ``` ## 浏览器支持 | IE | Firefox | Chrome | Safari | Opera | | --- | --- | --- | --- | --- | 所有浏览器都支持 background-attachment 属性。 注释:任何版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit"。 ## 定义和用法 background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动。 | 默认值: | scroll | | --- | --- | | 继承性: | no | | --- | --- | | 版本: | CSS1 | | --- | --- | | JavaScript 语法: | _object_.style.backgroundAttachment="fixed" | | --- | --- | ## 可能的值 | 值 | 描述 | | --- | --- | | scroll | 默认值。背景图像会随着页面其余部分的滚动而移动。 | | fixed | 当页面的其余部分滚动时,背景图像不会移动。 | | inherit | 规定应该从父元素继承 background-attachment 属性的设置。 | ## TIY 实例 [如何设置固定的背景图像](/tiy/t.asp?f=csse_background-attachment) 本例演示如何设置固定的背景图像。图像不会随着页面的其他部分滚动。 ``` <html> <head> <style type="text/css"> body { background-image:url(/i/eg_bg_02.gif); background-repeat:no-repeat; background-attachment:fixed } </style> </head> <body> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> <p>图像不会随页面的其余部分滚动。</p> </body> </html> ``` ## 相关页面 CSS 教程:[CSS 背景](/css/css_background.asp "CSS 背景") HTML DOM 参考手册:[backgroundAttachment 属性](/jsref/prop_style_backgroundattachment.asp "HTML DOM backgroundAttachment 属性")