aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/svg/applying_svg_effects_to_html_content/index.html
blob: a9f4d59fba100babc9683a150e2f2c927a2e73d2 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
---
title: 在 HTML 内容中应用 SVG 效果
slug: Web/SVG/Applying_SVG_effects_to_HTML_content
tags:
  - CSS
  - HTML
  - SVG
  - XHTML
  - 指南
  - 需要示例
translation_of: Web/SVG/Applying_SVG_effects_to_HTML_content
---
<p>现代浏览器支持在 <a href="/en-US/docs/Web/CSS" title="Cascading Style Sheets">CSS</a> 样式中使用 <a href="/en-US/docs/SVG">SVG</a> 来对HTML内容应用图像效果。</p>

<p>你可以在同一文件中使用SVG样式,也可以通过外部样式表引入。有三个属性可以使用: <a href="/en-US/docs/Web/CSS/mask"><code>mask</code></a>, <a href="/en-US/docs/Web/CSS/clip-path"><code>clip-path</code></a>, 和 <code><a href="/en-US/docs/Web/CSS/filter">filter</a></code></p>

<div class="note"><strong>注意:</strong> 在外部文件引入的SVG必须与原始文件 <a href="/en-US/docs/Web/Security/Same-origin_policy">同源</a></div>

<h2 id="使用内嵌SVG">使用内嵌SVG</h2>

<p>要想在CSS样式中应用SVG效果,首先需要创建一个引用SVG的CSS样式。</p>

<pre class="brush: html notranslate">&lt;style&gt;p { mask: url(#my-mask); }&lt;/style&gt;</pre>

<p>在上面的例子中, 所有段落会被<a href="/en-US/docs/Web/HTML/Global_attributes/id">ID</a> 为<code>my-mask的</code><a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/mask">SVG <code>&lt;mask&gt;</code></a>遮罩.</p>

<h3 id="例子_Masking">例子: Masking</h3>

<p>例如,你可以在你的HTML文档中用SVG和CSS代码对HTML内容作渐变mask效果。</p>

<pre class="brush: html notranslate">&lt;svg height="0"&gt;
  &lt;mask id="mask-1"&gt;
    &lt;linearGradient id="gradient-1" y2="1"&gt;
      &lt;stop stop-color="white" offset="0"/&gt;
      &lt;stop stop-opacity="0" offset="1"/&gt;
    &lt;/linearGradient&gt;
    &lt;circle cx="0.25" cy="0.25" r="0.25" id="circle" fill="white"/&gt;
    &lt;rect x="0.5" y="0.2" width="300" height="100" fill="url(#gradient-1)"/&gt;
  &lt;/mask&gt;
&lt;/svg&gt;
</pre>

<pre class="brush: css notranslate">.target {
  mask: url(#mask-1);
}
p {
  width: 300px;
  border: 1px solid #000;
  display: inline-block;
}</pre>

<p>注意,在CSS中 遮罩(mask) 使用一个指向 ID 为 <code>#mask-1</code>的URL,这个ID是在上面的SVG中指定的。 SVG中其他的内容指定了渐变遮罩的细节。</p>

<p>将SVG效果应用于 (X)HTML 是通过将 <code>target</code> 这个class应用于其他元素来实现的,如下所示:</p>

<pre class="brush: html notranslate">&lt;p class="target" style="background:lime;"&gt;
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua. Ut enim ad minim veniam.
&lt;/p&gt;
&lt;p&gt;
    Lorem ipsum dolor sit amet, consectetur adipisicing
    &lt;b class="target"&gt;elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.&lt;/b&gt;
    Ut enim ad minim veniam.
&lt;/p&gt;
</pre>

<p>上面的示例将渲染出一个有遮罩的例子</p>

<p>{{EmbedLiveSample('Example_Masking', 650, 200)}}</p>

<h3 id="例子_Clipping">例子: Clipping</h3>

<p>此示例演示如何使用SVG剪辑HTML内容。请注意,即使链接的可点击区域也被剪切。</p>

<pre class="brush: html notranslate">&lt;p class="target" style="background:lime;"&gt;
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua. Ut enim ad minim veniam.
&lt;/p&gt;
&lt;p&gt;
    Lorem ipsum dolor sit amet, consectetur adipisicing
    &lt;b class="target"&gt;elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.&lt;/b&gt;
    Ut enim ad minim veniam.
&lt;/p&gt;

&lt;button onclick="toggleRadius()"&gt;Toggle radius&lt;/button&gt;

&lt;svg height="0"&gt;
  &lt;clipPath id="clipping-path-1" clipPathUnits="objectBoundingBox"&gt;
    &lt;circle cx="0.25" cy="0.25" r="0.25" id="circle"/&gt;
    &lt;rect x="0.5" y="0.2" width="0.5" height="0.8"/&gt;
  &lt;/clipPath&gt;
&lt;/svg&gt;
</pre>

<pre class="brush: css notranslate">.target {
  clip-path: url(#clipping-path-1);
}
p {
  width: 300px;
  border: 1px solid #000;
  display: inline-block;
}</pre>

<p>这个例子将建立一个由圆形和矩形组成的剪切区域,为其指定 ID <code>#clipping-path-1</code>,然后在CSS中引用它。剪切路径可以分配给具有 <code>target</code> class的任何元素。</p>

<p>你可以实时地对SVG进行更改,并看到它们立即影响HTML的渲染。例如,可以在上面建立的剪切路径中调整圆形的大小:</p>

<pre class="brush: js notranslate">function toggleRadius() {
  var circle = document.getElementById("circle");
  circle.r.baseVal.value = 0.40 - circle.r.baseVal.value;
}
</pre>

<p>{{EmbedLiveSample('Example_Clipping', 650, 200)}}</p>

<h3 id="例子_Filtering">例子: Filtering</h3>

<p>这个例子演示了如何使用SVG对HTML内容进行过滤。它建立了几个过滤器,这些过滤器与CSS一起作用于正常和鼠标悬停状态 <a href="/en-US/docs/Web/CSS/:hover">hover</a> 下的三个元素。</p>

<pre class="brush: html notranslate">&lt;p class="target" style="background: lime;"&gt;
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua. Ut enim ad minim veniam.
&lt;/p&gt;
&lt;pre class="target"&gt;lorem&lt;/pre&gt;
&lt;p&gt;
    Lorem ipsum dolor sit amet, consectetur adipisicing
    &lt;b class="target"&gt;elit, sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.&lt;/b&gt;
    Ut enim ad minim veniam.
&lt;/p&gt;
</pre>

<p>任何SVG过滤器都可以这样使用。例如,要应用模糊效果,你可以这样使用:</p>

<pre class="brush: html notranslate">&lt;svg height="0"&gt;
  &lt;filter id="f1"&gt;
    &lt;feGaussianBlur stdDeviation="3"/&gt;
  &lt;/filter&gt;
&lt;/svg&gt;</pre>

<p>也可以应用于颜色矩阵:</p>

<pre class="brush: html notranslate">&lt;svg height="0"&gt;
  &lt;filter id="f2"&gt;
    &lt;feColorMatrix values="0.3333 0.3333 0.3333 0 0
                           0.3333 0.3333 0.3333 0 0
                           0.3333 0.3333 0.3333 0 0
                           0      0      0      1 0"/&gt;
  &lt;/filter&gt;
&lt;/svg&gt;
</pre>

<p>或更多的过滤器:</p>

<pre class="brush: html notranslate"><span>&lt;svg height="0"&gt;
</span>  &lt;filter id="f3"&gt;
    &lt;feConvolveMatrix filterRes="100 100" style="color-interpolation-filters:sRGB"
      order="3" kernelMatrix="0 -1 0   -1 4 -1   0 -1 0" preserveAlpha="true"/&gt;
  &lt;/filter&gt;
  &lt;filter id="f4"&gt;
    &lt;feSpecularLighting surfaceScale="5" specularConstant="1"
                        specularExponent="10" lighting-color="white"&gt;
      &lt;fePointLight x="-5000" y="-10000" z="20000"/&gt;
    &lt;/feSpecularLighting&gt;
  &lt;/filter&gt;
  &lt;filter id="f5"&gt;
    &lt;feColorMatrix values="1 0 0 0 0
                           0 1 0 0 0
                           0 0 1 0 0
                           0 1 0 0 0" style="color-interpolation-filters:sRGB"/&gt;
  &lt;/filter&gt;
<span>&lt;/svg&gt;</span></pre>

<p>使用以下CSS应用五个过滤器:</p>

<pre class="brush: css notranslate">p.target { filter:url(#f3); }
p.target:hover { filter:url(#f5); }
b.target { filter:url(#f1); }
b.target:hover { filter:url(#f4); }
pre.target { filter:url(#f2); }
pre.target:hover { filter:url(#f3); }
</pre>

<p>{{EmbedLiveSample('Example_Filtering', 650, 200)}}</p>

<p class="hidden"><a class="button liveSample" href="/files/3329/filterdemo.xhtml">View this example live</a></p>

<h3 id="例子_Blurred_Text">例子: Blurred Text</h3>

<p>为了模糊文本,基于Webkit的浏览器有一个名为blur的(前缀)CSS过滤器,(另见 <a href="/en-US/docs/Web/CSS/filter#blur%28%29_2">CSS filter</a>)。你可以使用SVG过滤器获得相同的效果。</p>

<pre class="brush: html notranslate">&lt;p class="blur"&gt;Time to clean my glasses&lt;/p&gt;
&lt;svg height="0"&gt;
  &lt;defs&gt;
    &lt;filter id="wherearemyglasses" x="0" y="0"&gt;
    &lt;feGaussianBlur in="SourceGraphic" stdDeviation="1"/&gt;
    &lt;/filter&gt;
  &lt;/defs&gt;
&lt;/svg&gt;
</pre>

<p>你可以在同一个class中使用SVG和CSS过滤器:</p>

<pre class="brush: css notranslate">.blur { filter: url(#wherearemyglasses); }</pre>

<p>{{ EmbedLiveSample('Example_Blurred_Text', 300, 100) }}</p>

<p>模糊的计算量很大,所以请谨慎使用它,尤其是在包含滚动或动画的元素中。</p>



<h3 id="例子_Text_Effects">例子: Text Effects</h3>

<p>SVG 还可以用于添加比纯HTML文本更动态、更灵活的文本添加方法。</p>

<p>通过使用SVG元素与HTML结合创建文本,你可以产生不同的文本的效果。如旋转文本:</p>

<pre class="notranslate">&lt;svg height="60" width="200"&gt;
  &lt;text x="0" y="15" fill="blue" transform="rotate(30 20,50)"&gt;Example text&lt;/text&gt;
&lt;/svg&gt;</pre>

<h2 id="使用外部引用">使用外部引用</h2>

<p>用来clipping,masking,filtering的SVG可以从其他外部源载入,只要外部源是与要使用SVG的该HTML文档同源的。</p>

<p>例如,CSS规则在一个名为default.css的文件中,如下这样:</p>

<pre class="brush: css notranslate" id="line1">.target { clip-path: url(resources.svg#c1); }</pre>

<p>这个SVG就可以从一个名为resources.svg的文件中导入,clip路径为ID c1。</p>

<h2 id="参见">参见</h2>

<ul>
 <li><a href="/en-US/docs/SVG" title="SVG">SVG</a></li>
 <li><a class="external" href="http://robert.ocallahan.org/2008/06/applying-svg-effects-to-html-content_04.html">SVG Effects for HTML Content</a> (blog post)</li>
 <li><del><a class="external" href="/web-tech/2008/10/10/svg-external-document-references">SVG External Document References</a></del> (blog post) (<a href="http://web.archive.org/web/20120512132948/https://developer.mozilla.org/web-tech/2008/10/10/svg-external-document-references/" title="Web Tech Blog » Blog Archive » SVG External Document References">[archive.org] Web Tech Blog » Blog Archive » SVG External Document References</a>)</li>
</ul>