Style backgroundAttachment 属性
Style 对象定义和用法
backgroundAttachment 属性设置或返回背景图像是否固定或者随着页面的其余部分滚动。
语法
设置 backgroundAttachment 属性:
Object.style.backgroundAttachment="scroll|fixed|local"
返回 backgroundAttachment 属性:
Object.style.backgroundAttachment
值 | 描述 |
---|---|
scroll | 背景随着元素一起滚动。这是默认。 |
fixed | 背景保持固定。 |
local | 背景随着元素的内容一起滚动。 |
浏览器支持
所有主要浏览器都支持 backgroundAttachment 属性。
实例
实例
设置背景图像为固定(不滚动):
<html>
<head>
<style type="text/css">
body
{
background:#f3f3f3 url('img_tree.png') no-repeat right top;
}
</style>
<script>
function displayResult()
{
document.body.style.backgroundAttachment="fixed";
}
</script>
</head>
<body>
<button type="button" onclick="displayResult()">Set background image to be fixed</button>
<br>
<h1>Hello World!</h1>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
</body>
</html>
<head>
<style type="text/css">
body
{
background:#f3f3f3 url('img_tree.png') no-repeat right top;
}
</style>
<script>
function displayResult()
{
document.body.style.backgroundAttachment="fixed";
}
</script>
</head>
<body>
<button type="button" onclick="displayResult()">Set background image to be fixed</button>
<br>
<h1>Hello World!</h1>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
<p>This is a paragraph</p>
</body>
</html>
更多实例
滚动和固定之间切换
滚动和固定的 backgroundAttachment 之间的切换。
Style 对象