1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
---
title: background-attachment
slug: Web/CSS/background-attachment
tags:
- CSS Background
- CSS Property
- CSS_参考
- Reference
translation_of: Web/CSS/background-attachment
---
<div>{{CSSRef}}</div>
<p><strong><code>background-attachment</code></strong> <a href="https://developer.mozilla.org/en-US/docs/CSS">CSS</a> 属性决定背景图像的位置是在{{glossary("视口")}}内固定,或者随着包含它的区块滚动。</p>
<div>{{EmbedInteractiveExample("pages/css/background-attachment.html")}}</div>
<p class="hidden">该交互式示例网页的源码存储在GitHub中,如果你想为该网页项目做点贡献的话,请进此链接 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> 并给我们一个请求加入的回复。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="brush: css no-line-numbers language-css"><code class="language-css"><span class="comment token">/* 关键 属性值 */</span>
<span class="property token">background-attachment</span><span class="punctuation token">:</span> scroll<span class="punctuation token">;</span>
<span class="property token">background-attachment</span><span class="punctuation token">:</span> fixed<span class="punctuation token">;</span>
<span class="property token">background-attachment</span><span class="punctuation token">:</span> local<span class="punctuation token">;</span>
<span class="comment token">/* 全局 属性值 */</span>
<span class="property token">background-attachment</span><span class="punctuation token">:</span> inherit<span class="punctuation token">;</span>
<span class="property token">background-attachment</span><span class="punctuation token">:</span> initial<span class="punctuation token">;</span>
<span class="property token">background-attachment</span><span class="punctuation token">:</span> unset<span class="punctuation token">;</span></code></pre>
<h3 id="取值" style="font-size: 1.71428571428571rem;">取值</h3>
<dl>
<dt><code>fixed</code></dt>
<dd>此关键属性值表示背景相对于视口固定。即使一个元素拥有滚动机制,背景也不会随着元素的内容滚动。</dd>
<dt><code>local</code></dt>
<dd>此关键属性值表示背景相对于元素的内容固定。如果一个元素拥有滚动机制,背景将会随着元素的内容滚动, 并且背景的绘制区域和定位区域是相对于可滚动的区域而不是包含他们的边框。</dd>
<dt><code>scroll</code></dt>
<dd>此关键属性值表示背景相对于元素本身固定, 而不是随着它的内容滚动(对元素边框是有效的)。</dd>
<dt>
<h3 id="标准语法" style="font-size: 1.71428571428571rem;">标准语法</h3>
</dt>
</dl>
{{csssyntax}}
<h2 id="例子">例子</h2>
<h3 id="简单的例子">简单的例子</h3>
<h4 id="CSS样式表">CSS样式表</h4>
<pre class="brush:css; highlight:[3];">p {
background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif");
background-attachment: fixed;
}
</pre>
<h4 id="HTML源码">HTML源码</h4>
<pre class="brush: html"><p>
There were doors all round the hall, but they were all locked; and when
Alice had been all the way down one side and up the other, trying every
door, she walked sadly down the middle, wondering how she was ever to
get out again.
</p></pre>
<h4 id="效果">效果</h4>
<p>{{EmbedLiveSample("简单的例子")}}</p>
<h3 id="多背景图支持">多背景图支持</h3>
<p>此属性支持多张背景图片。你可以用逗号分隔来为每一张背景图片指定不同的<code><attachment>属性值。</code>每一张背景图片顺序对应相应的 attachment 属性。</p>
<h4 id="CSS样式表_2">CSS样式表</h4>
<pre class="brush:css; highlight:[3];">p {
background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"), url("https://mdn.mozillademos.org/files/12059/startransparent.gif");
background-attachment: fixed, scroll;
background-repeat: no-repeat, repeat-y;
}</pre>
<h4 id="HTML源码_2">HTML源码</h4>
<pre class="brush: html"><p>
There were doors all round the hall, but they were all locked; and when
Alice had been all the way down one side and up the other, trying every
door, she walked sadly down the middle, wondering how she was ever to
get out again.
Suddenly she came upon a little three-legged table, all made of solid
glass; there was nothing on it except a tiny golden key, and Alice's
first thought was that it might belong to one of the doors of the hall;
but, alas! either the locks were too large, or the key was too small,
but at any rate it would not open any of them. However, on the second
time round, she came upon a low curtain she had not noticed before, and
behind it was a little door about fifteen inches high: she tried the
little golden key in the lock, and to her great delight it fitted!
</p></pre>
<h4 id="效果_2">效果</h4>
<p>{{EmbedLiveSample("多背景图支持")}}</p>
<h2 id="规范" style="margin-bottom: 20px; font-size: 2.14285714285714rem;">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">CSS版本</th>
<th scope="col">推荐状态</th>
<th scope="col">点评</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('CSS3 Backgrounds', '#the-background-attachment', 'background-attachment')}}</td>
<td>{{Spec2('CSS3 Backgrounds')}}</td>
<td>该简单上手的属性已经支持更多的背景图和局部(local)值</td>
</tr>
<tr>
<td>{{SpecName('CSS2.1', 'colors.html#propdef-background-attachment', 'background-attachment')}}</td>
<td>{{Spec2('CSS2.1')}}</td>
<td>无明显变化</td>
</tr>
<tr>
<td>{{SpecName('CSS1', '#background-attachment', 'background-attachment')}}</td>
<td>{{Spec2('CSS1')}}</td>
<td>无明显变化</td>
</tr>
</tbody>
</table>
<p>{{cssinfo}}</p>
<h2 id="浏览器兼容性" style="margin-bottom: 20px; font-size: 2.14285714285714rem;">浏览器兼容性</h2>
<p>{{Compat("css.properties.background-attachment")}}</p>
<div id="compat-mobile"></div>
<h2 id="参见" style="margin-bottom: 20px; font-size: 2.14285714285714rem;">参见</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/CSS/Multiple_backgrounds">更多背景图</a></li>
</ul>
|