构建自定义图像组件
Astro 提供了两个内置组件,可用于显示和优化图像。<Picture>
组件允许你显示响应式图像,并处理不同的格式和尺寸;而<Image>
组件则会优化你的图像,并允许你传入不同的格式和质量属性。
当你需要使用一些 <Picture>
和 <Image>
组件目前不支持的选项时,你可以使用 getImage()
函数来创建自定义组件。
在本示例中,你将使用 getImage()
函数 来创建自己的自定义图像组件,该组件基于媒体查询显示不同的源图像。
操作步骤
段落标题 操作步骤-
创建一个新的 Astro 组件,并导入
getImage()
函数src/components/MyCustomImageComponent.astro ---import { getImage } from "astro:assets";--- -
创建一个新的组件来处理自定义图片。
MyCustomComponent.astro
将会从Astro.props
中接收到三个props
。mobileImgUrl
和desktopImgUrl
属性用于在不同的视口尺寸下创建图片,而alt
属性用于设置图片的替代(alt)文本。这些属性将会在你渲染自定义图片组件的任何地方被传递。添加以下导入语句并定义你在组件中将使用的属性,你也可以使用 TypeScript 为属性添加类型定义。src/components/MyCustomImageComponent.astro ---import type { ImageMetadata } from "astro";import { getImage } from "astro:assets";interface Props {mobileImgUrl: string | ImageMetadata;desktopImgUrl: string | ImageMetadata;alt: string;}const { mobileImgUrl, desktopImgUrl, alt } = Astro.props;--- -
结合你需要的属性并调用
getImage()
函数来定义每个响应式图片。src/components/MyCustomImageComponent.astro ---import type { ImageMetadata } from "astro";import { getImage } from "astro:assets";interface Props {mobileImgUrl: string | ImageMetadata;desktopImgUrl: string | ImageMetadata;alt: string;}const { mobileImgUrl, desktopImgUrl, alt } = Astro.props;const mobileImg = await getImage({src: mobileImgUrl,format: "webp",width: 200,height: 200,});const desktopImg = await getImage({src: desktopImgUrl,format: "webp",width: 800,height: 200,});--- -
创建一个
<picture>
元素,它根据你期望的媒体查询生成包含不同图片的srcset
。src/components/MyCustomImageComponent.astro ---import type { ImageMetadata } from "astro";import { getImage } from "astro:assets";interface Props {mobileImgUrl: string | ImageMetadata;desktopImgUrl: string | ImageMetadata;alt: string;}const { mobileImgUrl, desktopImgUrl, alt } = Astro.props;const mobileImg = await getImage({src: mobileImgUrl,format: "webp",width: 200,height: 200,});const desktopImg = await getImage({src: desktopImgUrl,format: "webp",width: 800,height: 200,});---<picture><source media="(max-width: 799px)" srcset={mobileImg.src} /><source media="(min-width: 800px)" srcset={desktopImg.src} /><img src={desktopImg.src} alt={alt} /></picture> -
在任意
.astro
文件中导入并使用<MyCustomImageComponent />
组件。确保传递了在不同视口大小下生成两个不同图片所需的属性:src/pages/index.astro ---import MyCustomImageComponent from "../components/MyCustomImageComponent.astro";import mobileImage from "../images/mobile-profile-image.jpg";import desktopImage from "../images/desktop-profile-image.jpg";---<MyCustomImageComponentmobileImgUrl={mobileImage}desktopImgUrl={desktopImage}alt="user profile picture"/>
更多操作指南
-
状态共享
学习如何使用 Nano Stores 在框架组件之间共享状态
-
添加 RSS 摘要
通过向你的 Astro 网站添加 RSS 摘要让用户来订阅你的内容。
-
安装一个 Vite 或 Rollup 插件
了解如何通过向项目添加 Rollup 插件来导入 YAML 数据。
-
构建自定义图像组件
了解如何使用 getImage 函数构建支持媒体查询的自定义图像组件
-
使用 API 路由构建表单
了解如何使用 JavaScript 发送表单到 API 路由
-
在 Astro 页面中构建 HTML 表单
了解如何在 Astro 页面中构建 HTML 表单并在你的 frontmatter 中处理提交请求
-
在 Astro 中使用 Bun
了解如何在 Astro 站点中使用 Bun。
-
调用服务器端点
了解如何在 Astro 中调用服务器端点
-
校验验证码
了解如何创建一个 API 路由并从客户端进行获取。
-
用 Docker 来构建你的 Astro 网站
了解如何使用 Docker 来构建你的 Astro 网站。
-
Dynamically Import Images
Learn how to dynamically import images using Vite's import.meta.glob function
-
为链接添加图标
了解如何安装 rehype 插件并为你 Markdown 文件中的链接添加图标
-
添加 i18n 功能
通过动态路由和内容集合来让你的 Astro 站点支持国际化。
-
添加最后修改时间
编写一个在你的 Markdown 和 MDX 文件里添加最后修改时间的 remark 插件。
-
添加阅读时间
编写一个在你的 Markdown 和 MDX 文件里添加阅读所需时间的 remark 插件。
-
在 Astro 组件中共享状态
了解如何通过 Nano Stores 在 Astro 组件中共享状态
-
使用流式处理来提升页面性能
了解如何使用流式处理来提升页面性能
-
使用 Tailwind Typography 美化渲染后的 Markdown
了解如何使用 @tailwind/typography 美化你渲染后的 Markdown