Style backgroundAttachment 屬性

Style 對象參考手冊 Style 對象

定義和用法

backgroundAttachment 屬性設置或返回背景圖像是否固定或者隨著頁面的其餘部分滾動。

語法

設置 backgroundAttachment 屬性:

Object.style.backgroundAttachment="scroll|fixed|local"

返回 backgroundAttachment 屬性:

Object.style.backgroundAttachment

描述
scroll 背景隨著元素一起滾動。這是默認。
fixed 背景保持固定。
local 背景隨著元素的內容一起滾動。


流覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要流覽器都支持 backgroundAttachment 屬性。


實例

實例

設置背景圖像為固定(不滾動):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<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()">將背景圖像設置為固定的</button>
<br>
<h1>Hello World!</h1>
<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>



實例

更多實例

滾動和固定之間切換
滾動和固定的 backgroundAttachment 之間的切換。


Style 對象參考手冊 Style 對象