跳到正文
目录与搜索
⌘K

背景

Gradient 背景

一块在容器背后缓慢往返的巨大渐变,用来给页面增加深度;它只作为背景材质,不参与前景排版。

安装

背景是项目里的一个文件,用 shadcn CLI 添加,然后按需要改颜色与速度。

终端
bunx shadcn@latest add @animate-ui/backgrounds-gradient

用法

把背景放进一个定位容器,内容渲染在上层;容器需要自己处理溢出。

gradient.tsx
import { GradientBackground } from '@/components/animate-ui/backgrounds/gradient';

export function Hero() {
  return (
    <section className="relative h-[480px] overflow-hidden">
      <GradientBackground className="absolute inset-0" />
    </section>
  );
}

动效

  • 渐变的 background-position 每 15s 完成一次往返,全程平滑无跳变。
  • 离开视口或标签页隐藏时暂停;减少动效下停在当前帧。

所有背景都会在离开视口与标签页隐藏时暂停;减少动效时保留静止构图,不播放位移与循环。

API

属性类型默认值
transitionTransition{ duration: 15, ease: 'easeInOut', repeat: Infinity }
…propsHTMLMotionProps<'div'>—

背景接受容器 div 的全部属性,className 用来控制位置与尺寸;内容作为children 渲染在背景之上。

内容对应官方文档 2026-10-18 版本。