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 對象
更多建議: