aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/element()/index.html
blob: 57873c6e7d3506d059e1517180c46adfcfafb9b5 (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
---
title: element
slug: Web/CSS/element()
tags:
  - CSS
  - CSS Function
  - Layout
  - Web
  - 参考
  - 布局
translation_of: Web/CSS/element()
---
<div>{{CSSRef}}{{SeeCompatTable}}</div>

<p><a href="/en-US/docs/Web/CSS">CSS</a> 函数 <strong><code>element()</code></strong> 定义了一个从任意的 HTML 元素中生成的图像 {{cssxref("&lt;image&gt;")}} 值。该图像值是实时的,这意味着如果被指定的 HTML 元素被更改,the CSS properties using the resulting value are automatically updated.</p>

<p>一个特别实用的应用场景是,在某个 HTML {{HTMLElement("canvas")}} 元素中渲染图像,然后将其用作背景。</p>

<p>在使用 Gecko 渲染引擎的浏览器中,可以使用非标准的 {{domxref("document.mozSetImageElement()")}} 方法,将某个元素指定为其他元素的背景图像。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox notranslate">element(<var>id</var>)</pre>

<p>其中:</p>

<dl>
 <dt><var>id</var></dt>
 <dd>The ID of an element to use as the background, specified using the HTML attribute #<em>id</em> on the element.</dd>
</dl>

<h2 id="Examples" name="Examples">示例</h2>

<p>在支持 <code>-moz-element()</code> 的 Firefox 中,可以<a href="/samples/cssref/moz-element.html" title="https://developer.mozilla.org/samples/cssref/moz-element.html">实地考察</a>这些例子。</p>

<h3 id="一个比较现实的例子">一个比较现实的例子</h3>

<p>这个例子将文档中另一个隐藏的 {{HTMLElement("div")}} 元素作为背景。被隐藏的元素本身使用了渐变背景,也包含了一些文字,渐变背景和文字都成为了第一个元素的背景。</p>

<pre class="brush: html notranslate">&lt;div style="width:400px; height:400px; background:-moz-element(#myBackground1) no-repeat;"&gt;
  &lt;p&gt;This box uses the element with the #myBackground1 ID as its background!&lt;/p&gt;
&lt;/div&gt;

&lt;div style="overflow:hidden; height:0;"&gt;
  &lt;div id="myBackground1" style="width:1024px; height:1024px; background-image: linear-gradient(to right, red, orange, yellow, white);"&gt;
  &lt;p style="transform-origin:0 0; transform: rotate(45deg); color:white;"&gt;This text is part of the background. Cool, huh?&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;</pre>

<p>The {{HTMLElement("div")}} element with the ID "myBackground1" is used as the background for the content including the paragraph "This box uses the element with the #myBackground1 ID as its background!".</p>

<p><img alt="example1.png" class="default internal" src="/@api/deki/files/4624/=example1.png"></p>

<h3 id="一个有点怪怪的例子">一个有点怪怪的例子</h3>

<p>This example uses a hidden {{HTMLElement("button")}} element in a repeating pattern as its background. This demonstrates that you can use arbitrary elements as background, but doesn't necessarily demonstrate good design practices.</p>

<pre class="brush: html notranslate">&lt;div style="width:400px; height:100px; background:-moz-element(#myBackground2);"&gt;
&lt;/div&gt;

&lt;div style="overflow:hidden; height:0;"&gt;
  &lt;button id="myBackground2" type="button"&gt;Evil button!&lt;/button&gt;
&lt;/div&gt;
</pre>

<p><img alt="example2.png" class="default internal" src="/@api/deki/files/4625/=example2.png"></p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th>规范</th>
   <th>状态</th>
   <th>备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS4 Images', '#element-notation', 'Using Elements as Images: the element() notation')}}</td>
   <td>{{Spec2('CSS4 Images')}}</td>
   <td>Deferred from CSS3 Images.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>



<p>{{Compat("css.types.image.element")}}</p>

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

<ul>
 <li>{{cssxref("_image", "image()")}}</li>
 <li>{{cssxref("image-set", "image-set()")}}</li>
 <li>{{cssxref("&lt;image&gt;")}}</li>
 <li>{{cssxref("&lt;gradient&gt;")}}</li>
 <li>{{cssxref("element()")}}</li>
 <li>{{cssxref("cross-fade")}}</li>
 <li>{{domxref("document.mozSetImageElement()")}}</li>
</ul>