原语 / Animate UI
Image Zoom · Animate UI
在指针位置放大的图片,可悬停触发或点击触发。
安装
自有原语直接来自 @animate-ui/react,不依赖 Radix UI、Base UI 或 React Aria;装上包即可使用。
bun add @animate-ui/react motion用法
直接从 @animate-ui/react 的对应子路径引入,下面就是完整示例;样式由你的类名决定。
import { Image, ImageZoom } from '@animate-ui/react/primitives/effects/image-zoom';
export function ProductPhoto() {
return (
<ImageZoom width={320} height={320} aria-label="Product photo, zoomable">
<Image src="/product.webp" alt="The product" />
</ImageZoom>
);
}动效
- 悬停时以指针所在点为中心放大到 zoomScale,弹簧收敛;移动指针会平移放大区域。
- 离开后回到原始比例;触屏设备用点击切换;减少动效下直接切换比例。
所有时长都会乘以 AnimateUIProvider 的 tempo,并遵守系统的减少动效设置。
API
动效相关的 transition 与 exitTransition 是 Animate UI 唯一新增的属性;其余 props 见上表。
| 属性 | 类型 | 默认值 |
|---|---|---|
| zoomScale | number | 3 |
| transition | Transition | { type: 'spring', stiffness: 200, damping: 28 } |
| zoomOnHover | boolean | true |
| zoomOnClick | boolean | true |
| disabled | boolean | false |
| width | React.CSSProperties['width'] | '100%' |
| height | React.CSSProperties['height'] | '100%' |
| children | React.ReactElement | — |
| …props | React.ComponentProps<'div'> | — |
| objectFit | React.CSSProperties['objectFit'] | 'cover' |
| as | React.ElementType | 'img' |
| …props | React.ComponentProps<T> | — |
未列出的属性与上游 API 完全一致,可在 官方文档 查阅。
内容对应官方文档 2026-10-18 版本。