Skip to content

Commit 9272156

Browse files
i18n(zh-cn): Update astro-assets.mdx (#12478)
Co-authored-by: Yan <[email protected]>
1 parent 98c3e11 commit 9272156

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

src/content/docs/zh-cn/reference/modules/astro-assets.mdx

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ import {
2727

2828
### `<Image />`
2929

30+
`<Image />` 组件能优化并转换图像。
31+
32+
该组件还可用于创建能根据容器尺寸或设备屏幕大小及分辨率自动调整的 [响应式图片](#响应式图片属性)
33+
3034
```astro title="src/components/MyComponent.astro"
3135
---
3236
// 导入图像组件和图片
3337
import { Image } from 'astro:assets';
34-
import myImage from "../assets/my_image.png"; // Image is 1600x900
38+
import myImage from "../assets/my_image.png"; // 图片分辨率是 1600x900
3539
---
3640
3741
<!-- `alt` 在 Image 组件中是必需的属性 -->
@@ -53,7 +57,7 @@ import myImage from "../assets/my_image.png"; // Image is 1600x900
5357

5458
#### Image 属性
5559

56-
除了下面描述的属性之外,`<Image />` 组件还接受 HTML `<img>` 标签接受的所有属性
60+
`<Image />` 组件除接受 HTML `<img>` 标签的所有属性外,还支持以下列出的属性及 [响应式图片属性](#响应式图片属性)
5761

5862
##### src (必须)
5963

@@ -122,6 +126,8 @@ import myImage from "../assets/my_image.png"; // Image is 1600x900
122126

123127
这些属性定义了要用于图像的尺寸。
124128

129+
当设置了 `layout` 类型时,这些属性会根据图像尺寸自动生成,在大多数情况下不应手动设置。
130+
125131
当使用保持原始宽高比的图像时,`width``height` 是可选的。这些尺寸可以自动从位于 `src/` 中的图像文件自动推断出来。而对于远程图像,请将 `<Image />``<Picture />` 组件上的 [`inferSize` 属性设置为 `true`](#infersize),或使用 [`inferRemoteSize()`](#inferremotesize) 函数。
126132

127133
但是,对于存储在你的 `public/` 文件夹中的图像,这两个属性都是必需的,因为 Astro 无法分析这些文件。
@@ -136,6 +142,8 @@ import myImage from "../assets/my_image.png"; // Image is 1600x900
136142

137143
为图像生成的像素密度列表。
138144

145+
`densities` 属性与设置了 `layout` 属性或 `image.layout` 配置的 [响应式图像](#响应式图片属性) 不兼容,若同时设置将被忽略。
146+
139147
如果提供了该值,将会在 `<img>` 标签上生成一个 `srcset` 属性。在使用此值时,请不要提供 `widths` 的值。
140148

141149
与原始图像相同或大于原始图像宽度的像素密度会被忽略,以避免放大图像。
@@ -181,6 +189,8 @@ import myImage from '../assets/my_image.png';
181189

182190
如果提供了该值,将会在 `<img>` 标签上生成一个 `srcset` 属性。但还需提供一个 [`sizes` 属性](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/sizes)
183191

192+
`widths``sizes` 属性将通过 `layout` 属性自动生成,用于响应式图像。通常无需提供这些值,但可用于覆盖任何自动生成的值。
193+
184194
在使用该值时,请不要提供 `densities` 的值。这两个值只能选择一个来生成 `srcset` 属性。
185195

186196
将忽略大于原始图像宽度的宽度值,以避免对图像进行放大。
@@ -222,6 +232,20 @@ import myImage from '../assets/my_image.png'; // Image is 1600x900
222232
/>
223233
```
224234

235+
##### sizes
236+
237+
<p>
238+
239+
**类型:** `string | undefined`<br />
240+
<Since v="3.3.0" />
241+
</p>
242+
243+
指定图像在各类媒体查询条件下的布局宽度。在指定 `widths` 时必须提供此参数。
244+
245+
响应式图片的 `widths``sizes` 属性将通过布局属性自动生成。通常无需手动提供这些值,但可用于覆盖任何自动生成的值。
246+
247+
针对 `constrained``full-width` 图片自动生成的 `sizes` 属性,其计算前提是当视口宽度小于图片宽度时,图片会以接近屏幕全宽的方式显示。若实际布局与此存在显著差异(例如在小屏幕设备上采用多栏布局),则可能需要手动调整 `sizes` 属性以获得最佳效果。
248+
225249
##### format
226250

227251
<p>
@@ -249,6 +273,7 @@ import myImage from '../assets/my_image.png'; // Image is 1600x900
249273
<p>
250274

251275
**类型:** `boolean`<br />
276+
**默认值:** `false`<br />
252277
<Since v="4.4.0" />
253278
</p>
254279

@@ -258,7 +283,7 @@ import myImage from '../assets/my_image.png'; // Image is 1600x900
258283

259284
`<Image />` 组件中添加 `inferSize`(或者在 `getImage()` 中添加 `inferSize: true`),以便在获取时从图片内容中推断这些值。如果你不知道远程图片的尺寸,或者这些尺寸可能会变化,这个属性对你会很有帮助:
260285

261-
```astro mark="inferSize"
286+
```astro title="src/components/MyComponent.astro" "inferSize"
262287
---
263288
import { Image } from 'astro:assets';
264289
---
@@ -267,11 +292,43 @@ import { Image } from 'astro:assets';
267292

268293
`inferSize` 能够获取来自未经授权域的 [远程图片的尺寸](/zh-cn/guides/images/#授权远程图像),但图片本身将保持未处理状态。
269294

295+
296+
##### priority
297+
298+
<p>
299+
300+
**类型:** `boolean`<br />
301+
**默认值:** `false`<br />
302+
<Since v="5.10.0" />
303+
</p>
304+
305+
可自动为首屏图片设置 `loading``decoding`,和 `fetchpriority` 属性至最佳值。
306+
307+
```astro title="src/components/MyComponent.astro" "priority"
308+
---
309+
import { Image } from 'astro:assets';
310+
import myImage from '../assets/my_image.png';
311+
---
312+
<Image src={myImage} priority alt="A description of my image" />
313+
```
314+
315+
当在 `<Image />``<Picture />` 组件中添加 `priority="true"`(或简写语法 `priority` )时,它将添加以下属性以指示浏览器立即加载图像:
316+
317+
```html
318+
loading="eager"
319+
decoding="sync"
320+
fetchpriority="high"
321+
```
322+
323+
如需进一步定制,这些独立属性仍可手动设置。
324+
270325
### `<Picture />`
271326

272327
<p><Since v="3.3.0" /></p>
273328

274-
使用内置的 `<Picture />` Astro 组件来展示具有多种格式和(或)尺寸的响应式图像。
329+
`<Picture />` 组件可以生成具有多种格式和(或)尺寸的优化后的图像。
330+
331+
该组件还可用于创建能根据容器尺寸或设备屏幕大小及分辨率自动调整的 [响应式图片](#响应式图片属性)
275332

276333
```astro title="src/pages/index.astro"
277334
---
@@ -301,7 +358,7 @@ import myImage from "../assets/my_image.png"; // 图像的分辨率为 1600x900
301358

302359
#### Picture 属性
303360

304-
`<Picture />` 接受 [`<Image />` 组件](#image-属性) 的所有属性,以及以下属性:
361+
`<Picture />` 接受 [`<Image />` 组件](#image-属性) 的所有属性(包括 [响应式图片属性](#响应式图片属性),以及以下属性:
305362

306363
##### `formats`
307364

@@ -478,17 +535,6 @@ import myImage from '../assets/my_image.png';
478535

479536
值与 CSS `object-position` 的值相匹配。默认为 `center`,如果设置了,则为 [`image.objectPosition`](/zh-cn/reference/configuration-reference/#imageobjectposition) 的值。可用于覆盖默认的 `object-position` 样式。
480537

481-
##### priority
482-
483-
<p>
484-
485-
**类型:** `boolean` <br />
486-
**默认值:** `false` <br />
487-
<Since v="5.10.0" />
488-
</p>
489-
490-
当设置或配置了 [`layout`](#layout) 属性时启用。如果设置,则会立即加载响应式图片。否则,图片将被延迟加载。用于首屏中最大的图片。默认为 `false`
491-
492538
### `getImage()`
493539

494540
<p>

0 commit comments

Comments
 (0)