js-components-basic-image.md 10.6 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# image<a name="ZH-CN_TOPIC_0000001173164699"></a>

图片组件,用来渲染展示图片。

## 子组件<a name="section9288143101012"></a>

不支持。

## 属性<a name="section2907183951110"></a>

除支持[通用属性](js-components-common-attributes.md)外,还支持如下属性:

Z
zengyawen 已提交
13
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="23.119999999999997%" id="mcps1.1.6.1.1"><p>名称</p>
Z
zengyawen 已提交
14
</th>
Z
zengyawen 已提交
15
<th class="cellrowborder" valign="top" width="23.119999999999997%" id="mcps1.1.6.1.2"><p>类型</p>
Z
zengyawen 已提交
16
</th>
Z
zengyawen 已提交
17
<th class="cellrowborder" valign="top" width="10.48%" id="mcps1.1.6.1.3"><p>默认值</p>
Z
zengyawen 已提交
18
</th>
Z
zengyawen 已提交
19
<th class="cellrowborder" valign="top" width="7.5200000000000005%" id="mcps1.1.6.1.4"><p>必填</p>
Z
zengyawen 已提交
20
</th>
Z
zengyawen 已提交
21
<th class="cellrowborder" valign="top" width="35.76%" id="mcps1.1.6.1.5"><p>描述</p>
Z
zengyawen 已提交
22 23 24
</th>
</tr>
</thead>
Z
zengyawen 已提交
25
<tbody><tr><td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.1 "><p>src</p>
Z
zengyawen 已提交
26
</td>
Z
zengyawen 已提交
27
<td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
28
</td>
Z
zengyawen 已提交
29
<td class="cellrowborder" valign="top" width="10.48%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
30
</td>
Z
zengyawen 已提交
31
<td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
32
</td>
Z
zengyawen 已提交
33 34 35
<td class="cellrowborder" valign="top" width="35.76%" headers="mcps1.1.6.1.5 "><p><span>图片的路径,支持本地路径,图片格式包括png、jpg、bmp、svg和gif。</span></p>
<p>支持Base64字符串<sup><span>6+</span></sup>。格式为data:image/[png | jpeg | bmp | webp];base64, [base64 data], 其中[base64 data]为Base64字符串数据。</p>
<p>支持dataability://的路径前缀,用于访问通过data ability提供的图片路径<sup><span>6+</span></sup></p>
Z
zengyawen 已提交
36 37
</td>
</tr>
Z
zengyawen 已提交
38
<tr><td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.1 "><p>alt</p>
Z
zengyawen 已提交
39
</td>
Z
zengyawen 已提交
40
<td class="cellrowborder" valign="top" width="23.119999999999997%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
41
</td>
Z
zengyawen 已提交
42
<td class="cellrowborder" valign="top" width="10.48%" headers="mcps1.1.6.1.3 "><p>-</p>
Z
zengyawen 已提交
43
</td>
Z
zengyawen 已提交
44
<td class="cellrowborder" valign="top" width="7.5200000000000005%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
45
</td>
Z
zengyawen 已提交
46
<td class="cellrowborder" valign="top" width="35.76%" headers="mcps1.1.6.1.5 "><p>占位图,当指定图片在加载中时显示。</p>
Z
zengyawen 已提交
47 48 49 50 51 52 53 54 55
</td>
</tr>
</tbody>
</table>

## 样式<a name="section45171131134215"></a>

除支持[通用样式](js-components-common-styles.md)外,还支持如下属性:

Z
zengyawen 已提交
56
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="23.16768323167683%" id="mcps1.1.6.1.1"><p>名称</p>
Z
zengyawen 已提交
57
</th>
Z
zengyawen 已提交
58
<th class="cellrowborder" valign="top" width="20.427957204279572%" id="mcps1.1.6.1.2"><p>类型</p>
Z
zengyawen 已提交
59
</th>
Z
zengyawen 已提交
60
<th class="cellrowborder" valign="top" width="8.869113088691131%" id="mcps1.1.6.1.3"><p>默认值</p>
Z
zengyawen 已提交
61
</th>
Z
zengyawen 已提交
62
<th class="cellrowborder" valign="top" width="7.519248075192481%" id="mcps1.1.6.1.4"><p>必填</p>
Z
zengyawen 已提交
63
</th>
Z
zengyawen 已提交
64
<th class="cellrowborder" valign="top" width="40.01599840015999%" id="mcps1.1.6.1.5"><p>描述</p>
Z
zengyawen 已提交
65 66 67
</th>
</tr>
</thead>
Z
zengyawen 已提交
68
<tbody><tr><td class="cellrowborder" valign="top" width="23.16768323167683%" headers="mcps1.1.6.1.1 "><p>object-fit</p>
Z
zengyawen 已提交
69
</td>
Z
zengyawen 已提交
70
<td class="cellrowborder" valign="top" width="20.427957204279572%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
71
</td>
Z
zengyawen 已提交
72
<td class="cellrowborder" valign="top" width="8.869113088691131%" headers="mcps1.1.6.1.3 "><p>cover</p>
Z
zengyawen 已提交
73
</td>
Z
zengyawen 已提交
74
<td class="cellrowborder" valign="top" width="7.519248075192481%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
75
</td>
Z
zengyawen 已提交
76
<td class="cellrowborder" valign="top" width="40.01599840015999%" headers="mcps1.1.6.1.5 "><p>设置图片的缩放类型。可选值类型说明请见<a href="#table175851535113711">object-fit 类型说明</a>。(不支持svg格式)</p>
Z
zengyawen 已提交
77 78
</td>
</tr>
Z
zengyawen 已提交
79
<tr><td class="cellrowborder" valign="top" width="23.16768323167683%" headers="mcps1.1.6.1.1 "><p>match-text-direction</p>
Z
zengyawen 已提交
80
</td>
Z
zengyawen 已提交
81
<td class="cellrowborder" valign="top" width="20.427957204279572%" headers="mcps1.1.6.1.2 "><p>boolean</p>
Z
zengyawen 已提交
82
</td>
Z
zengyawen 已提交
83
<td class="cellrowborder" valign="top" width="8.869113088691131%" headers="mcps1.1.6.1.3 "><p>false</p>
Z
zengyawen 已提交
84
</td>
Z
zengyawen 已提交
85
<td class="cellrowborder" valign="top" width="7.519248075192481%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
86
</td>
Z
zengyawen 已提交
87
<td class="cellrowborder" valign="top" width="40.01599840015999%" headers="mcps1.1.6.1.5 "><p>图片是否跟随文字方向。(不支持svg格式)</p>
Z
zengyawen 已提交
88 89
</td>
</tr>
Z
zengyawen 已提交
90
<tr><td class="cellrowborder" valign="top" width="23.16768323167683%" headers="mcps1.1.6.1.1 "><p>fit-original-size</p>
Z
zengyawen 已提交
91
</td>
Z
zengyawen 已提交
92
<td class="cellrowborder" valign="top" width="20.427957204279572%" headers="mcps1.1.6.1.2 "><p>boolean</p>
Z
zengyawen 已提交
93
</td>
Z
zengyawen 已提交
94
<td class="cellrowborder" valign="top" width="8.869113088691131%" headers="mcps1.1.6.1.3 "><p>false</p>
Z
zengyawen 已提交
95
</td>
Z
zengyawen 已提交
96
<td class="cellrowborder" valign="top" width="7.519248075192481%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
97
</td>
Z
zengyawen 已提交
98
<td class="cellrowborder" valign="top" width="40.01599840015999%" headers="mcps1.1.6.1.5 "><p>image组件在未设置宽高的情况下是否适应图源尺寸(该属性为true时object-fit属性不生效),svg类型图源不支持该属性。</p>
Z
zengyawen 已提交
99 100
</td>
</tr>
Z
zengyawen 已提交
101
<tr><td class="cellrowborder" valign="top" width="23.16768323167683%" headers="mcps1.1.6.1.1 "><p>object-position<sup>7+</sup></p>
Z
zengyawen 已提交
102
</td>
Z
zengyawen 已提交
103
<td class="cellrowborder" valign="top" width="20.427957204279572%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
104
</td>
Z
zengyawen 已提交
105
<td class="cellrowborder" valign="top" width="8.869113088691131%" headers="mcps1.1.6.1.3 "><p>0px 0px</p>
Z
zengyawen 已提交
106
</td>
Z
zengyawen 已提交
107
<td class="cellrowborder" valign="top" width="7.519248075192481%" headers="mcps1.1.6.1.4 "><p></p>
Z
zengyawen 已提交
108
</td>
Z
zengyawen 已提交
109 110 111 112 113
<td class="cellrowborder" valign="top" width="40.01599840015999%" headers="mcps1.1.6.1.5 "><p>设置图片在组件内展示的位置。</p>
<p>设置类型有两种:</p>
<p>1. 像素,单位px,示例 15px 15px 代表X轴或者Y轴移动的位置</p>
<p>2. 字符,可选值:</p>
<ul><li>left 图片显示在组件左侧;</li><li>top 图片显示在组件顶部位置;</li><li>right 图片显示在组件右侧位置;</li><li>bottom图片显示在组件底部位置。</li></ul>
Z
zengyawen 已提交
114 115 116 117 118 119 120 121
</td>
</tr>
</tbody>
</table>

**表 1**  object-fit 类型说明

<a name="table175851535113711"></a>
Z
zengyawen 已提交
122
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="15.02%" id="mcps1.2.3.1.1"><p>类型</p>
Z
zengyawen 已提交
123
</th>
Z
zengyawen 已提交
124
<th class="cellrowborder" valign="top" width="84.98%" id="mcps1.2.3.1.2"><p>描述</p>
Z
zengyawen 已提交
125 126 127
</th>
</tr>
</thead>
Z
zengyawen 已提交
128
<tbody><tr><td class="cellrowborder" valign="top" width="15.02%" headers="mcps1.2.3.1.1 "><p>cover</p>
Z
zengyawen 已提交
129
</td>
Z
zengyawen 已提交
130
<td class="cellrowborder" valign="top" width="84.98%" headers="mcps1.2.3.1.2 "><p>保持宽高比进行缩小或者放大,使得图片两边都大于或等于显示边界,居中显示。</p>
Z
zengyawen 已提交
131 132
</td>
</tr>
Z
zengyawen 已提交
133
<tr><td class="cellrowborder" valign="top" width="15.02%" headers="mcps1.2.3.1.1 "><p>contain</p>
Z
zengyawen 已提交
134
</td>
Z
zengyawen 已提交
135
<td class="cellrowborder" valign="top" width="84.98%" headers="mcps1.2.3.1.2 "><p>保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内,居中显示。</p>
Z
zengyawen 已提交
136 137
</td>
</tr>
Z
zengyawen 已提交
138
<tr><td class="cellrowborder" valign="top" width="15.02%" headers="mcps1.2.3.1.1 "><p>fill</p>
Z
zengyawen 已提交
139
</td>
Z
zengyawen 已提交
140
<td class="cellrowborder" valign="top" width="84.98%" headers="mcps1.2.3.1.2 "><p>不保持宽高比进行放大缩小,使得图片填充满显示边界。</p>
Z
zengyawen 已提交
141 142
</td>
</tr>
Z
zengyawen 已提交
143
<tr><td class="cellrowborder" valign="top" width="15.02%" headers="mcps1.2.3.1.1 "><p>none</p>
Z
zengyawen 已提交
144
</td>
Z
zengyawen 已提交
145
<td class="cellrowborder" valign="top" width="84.98%" headers="mcps1.2.3.1.2 "><p>保持原有尺寸进行居中显示。</p>
Z
zengyawen 已提交
146 147
</td>
</tr>
Z
zengyawen 已提交
148
<tr><td class="cellrowborder" valign="top" width="15.02%" headers="mcps1.2.3.1.1 "><p>scale-down</p>
Z
zengyawen 已提交
149
</td>
Z
zengyawen 已提交
150
<td class="cellrowborder" valign="top" width="84.98%" headers="mcps1.2.3.1.2 "><p>保持宽高比居中显示,图片缩小或者保持不变。</p>
Z
zengyawen 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
</td>
</tr>
</tbody>
</table>

>![](../../public_sys-resources/icon-note.gif) **说明:** 
>使用svg图片资源时:
>-   建议设置image组件的长宽,否则在父组件的长或宽为无穷大的场景下,svg资源将不会绘制;
>-   如果svg描述中未指定相应的长宽,则svg将会填满image组件区域;
>-   如果svg描述中指定了相应的长宽,和image组件本身的长宽效果如下:
>1.  如果image组件本身的长宽小于svg中的长宽,svg会被裁切,仅显示左上角部分;
>2.  如果image组件本身的长宽大于svg中的长宽,svg会被放置在image组件的左上角,image组件其他部分显示空白。

## 事件<a name="section3892191911214"></a>

除支持[通用事件](js-components-common-events.md)外,还支持如下事件:

Z
zengyawen 已提交
168
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="24.91249124912491%" id="mcps1.1.4.1.1"><p>名称</p>
Z
zengyawen 已提交
169
</th>
Z
zengyawen 已提交
170
<th class="cellrowborder" valign="top" width="29.492949294929495%" id="mcps1.1.4.1.2"><p>参数</p>
Z
zengyawen 已提交
171
</th>
Z
zengyawen 已提交
172
<th class="cellrowborder" valign="top" width="45.5945594559456%" id="mcps1.1.4.1.3"><p>描述</p>
Z
zengyawen 已提交
173 174 175
</th>
</tr>
</thead>
Z
zengyawen 已提交
176
<tbody><tr><td class="cellrowborder" valign="top" width="24.91249124912491%" headers="mcps1.1.4.1.1 "><p>complete(Rich)</p>
Z
zengyawen 已提交
177
</td>
Z
zengyawen 已提交
178
<td class="cellrowborder" valign="top" width="29.492949294929495%" headers="mcps1.1.4.1.2 "><p>{ width:width, height:height }</p>
Z
zengyawen 已提交
179
</td>
Z
zengyawen 已提交
180
<td class="cellrowborder" valign="top" width="45.5945594559456%" headers="mcps1.1.4.1.3 "><p>图片成功加载时触发该回调,返回成功加载的图源尺寸。</p>
Z
zengyawen 已提交
181 182
</td>
</tr>
Z
zengyawen 已提交
183
<tr><td class="cellrowborder" valign="top" width="24.91249124912491%" headers="mcps1.1.4.1.1 "><p>error(Rich)</p>
Z
zengyawen 已提交
184
</td>
Z
zengyawen 已提交
185
<td class="cellrowborder" valign="top" width="29.492949294929495%" headers="mcps1.1.4.1.2 "><p>{ width:width, height:height }</p>
Z
zengyawen 已提交
186
</td>
Z
zengyawen 已提交
187
<td class="cellrowborder" valign="top" width="45.5945594559456%" headers="mcps1.1.4.1.3 "><p>图片加载出现异常时触发该回调,异常时长宽为零。</p>
Z
zengyawen 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201
</td>
</tr>
</tbody>
</table>

## 方法<a name="section2279124532420"></a>

支持[通用方法](js-components-common-methods.md)

## 示例<a name="section321147145519"></a>

```
<!-- xxx.hml -->
<div class="container">
Z
zengyawen 已提交
202
  <image src="common/images/hw_right.png" style="width: 300px; height: 300px; object-fit:{{fit}}; object-position: center center; border: 1px solid red; ">
Z
zengyawen 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
  </image>
  <select class="selects" onchange="change_fit">
    <option for="{{fits}}" value="{{$item}}">{{$item}}</option>
  </select>
</div>
```

```
/* xxx.css */
.container {
  justify-content: center;
  align-items: center;
  flex-direction: column;
Z
zengyawen 已提交
216 217
  
  
Z
zengyawen 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
}
.selects{
  margin-top: 20px;
  width:300px;
  border:1px solid #808080;
  border-radius: 10px;
}
```

```
// xxx.js
export default {
  data: {
    fit:"cover",
    fits: ["cover", "contain", "fill", "none", "scale-down"],
  },
  change_fit(e) {
    this.fit = e.newValue;
  },
}
```

![](figures/GIF.gif)