原语 / Animate UI
Magnetic · Animate UI
指针靠近时被吸过去的元素,距离越近吸力越强。
安装
自有原语直接来自 @animate-ui/react,不依赖 Radix UI、Base UI 或 React Aria;装上包即可使用。
bun add @animate-ui/react motion用法
直接从 @animate-ui/react 的对应子路径引入,下面就是完整示例;样式由你的类名决定。
import { Magnetic } from '@animate-ui/react/primitives/effects/magnetic';
export function CallToAction() {
return (
<Magnetic strength={0.4} range={160}>
<button>Come closer</button>
</Magnetic>
);
}动效
- 在 range 像素范围内,元素朝指针移动,强度由 strength 控制;距离从元素当前位置计算,包含已经被吸走的位移。
- 指针离开范围后弹回原位;减少动效下不移动。
所有时长都会乘以 AnimateUIProvider 的 tempo,并遵守系统的减少动效设置。
API
动效相关的 transition 与 exitTransition 是 Animate UI 唯一新增的属性;其余 props 见上表。
| 属性 | 类型 | 默认值 |
|---|---|---|
| asChild | boolean | false |
| strength | number | 0.5 |
| range | number | 120 |
| springOptions | SpringOptions | { stiffness: 100, damping: 10, mass: 0.5 } |
| onlyOnHover | boolean | false |
| disableOnTouch | boolean | true |
| …props | HTMLMotionProps<'div'> | — |
未列出的属性与上游 API 完全一致,可在 官方文档 查阅。
内容对应官方文档 2026-10-18 版本。