Latest web development tutorials

HTML DOM Style backgroundAttachment property

Style Object Reference Style Objects

Definition and Usage

backgroundAttachment property sets or returns the background image is fixed or scrolling with the rest of the page.

grammar

Setting backgroundAttachment properties:

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

Back backgroundAttachment properties:

Object.style.backgroundAttachment

描述
scroll 背景随着元素一起滚动。这是默认。
fixed 背景保持固定。
local 背景随着元素的内容一起滚动。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support backgroundAttachment property.


Examples

Examples

Background image is fixed (does not scroll):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.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>

try it"


Examples

More examples

And switching between the fixed scroll
Scrolling and switching between backgroundAttachment fixed.


Style Object Reference Style Objects