Communitygithub.com

Kiddo4993/Claude-Skills

实现滚动驱动动画——视差、滚动显示、进度指示器、粘性头部以及用于机构级和产品级网站的 GSAP ScrollTrigger 序列。

Claude-Skills 是什么?

该技能专注于在 React 项目(包括 Framer Motion 和 GSAP)中实现高性能的滚动驱动动画。它提供了现成的组件和模式,用于创建视差效果、滚动进入时的淡入滑动动画、滚动进度条、钉住水平滚动(类似 Awwwards 风格)以及逐行文本揭示动画。该技能强调正确的性能实践,如仅对 transform 和 opacity 进行动画处理、使用 will-change 以及正确清理 ScrollTrigger 实例。它支持两种主要方法:使用 Framer Motion 的 useScroll 和 useTransform 处理简单的单元素动画,以及使用 GSAP 的 ScrollTrigger 处理涉及钉住和擦除的复杂时间线。本技能中提供的代码示例可直接复制粘贴到基于 React 的项目中,主要面向构建高端营销网站和产品界面的开发者。

兼容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Kiddo4993/Claude-Skills/tree/HEAD/scroll-animations

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

预览

来自技能 README

Claude-Skills preview

文档

滚动动画技能——机构级 / 产品级品质

滚动动画是高预算网站赢得其预算的方式。做得好的话,它们会给人电影般的感受。做得不好,则会导致晕动症。本技能正确地实现它们。

两种方法

使用场景工具
React 组件,单个元素Framer Motion useScroll + useTransform
复杂时间线,钉住序列,擦除GSAP ScrollTrigger

Framer Motion:滚动进入时的淡入 + 滑动

import { motion, useInView } from 'framer-motion'
import { useRef } from 'react'

function RevealOnScroll({ children, delay = 0 }: { children: React.ReactNode; delay?: number }) {
  const ref = useRef(null)
  const isInView = useInView(ref, { once: true, margin: '-80px 0px' })

  return (
    <motion.div
      ref={ref}
      initial={{ opacity: 0, y: 32, filter: 'blur(6px)' }}
      animate={isInView ? { opacity: 1, y: 0, filter: 'blur(0px)' } : {}}
      transition={{ type: 'spring', stiffness: 200, damping: 24, delay }}
    >
      {children}
    </motion.div>
  )
}

Framer Motion:滚动进度条

import { useScroll, useSpring, motion } from 'framer-motion'

function ScrollProgressBar() {
  const { scrollYProgress } = useScroll()
  const scaleX = useSpring(scrollYProgress, { stiffness: 100, damping: 30 })

  return (
    <motion.div
      className="fixed top-0 left-0 right-0 h-[3px] bg-blue-500 origin-left z-50"
      style={{ scaleX }}
    />
  )
}

Framer Motion:视差英雄图像

import { useScroll, useTransform, motion } from 'framer-motion'
import { useRef } from 'react'

function ParallaxHero() {
  const ref = useRef(null)
  const { scrollYProgress } = useScroll({ target: ref, offset: ['start start', 'end start'] })
  const y = useTransform(scrollYProgress, [0, 1], ['0%', '30%'])
  const opacity = useTransform(scrollYProgress, [0, 0.6], [1, 0])

  return (
    <section ref={ref} className="relative h-screen overflow-hidden">
      <motion.div className="absolute inset-0" style={{ y }}>
        <img src="/hero.jpg" className="w-full h-full object-cover" alt="" />
      </motion.div>
      <motion.div className="relative z-10 flex items-center justify-center h-full" style={{ opacity }}>
        <h1 className="text-white text-7xl font-bold">Hero Text</h1>
      </motion.div>
    </section>
  )
}

GSAP ScrollTrigger:钉住水平滚动(Awwwards 风格)

import { useEffect, useRef } from 'react'
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'

gsap.registerPlugin(ScrollTrigger)

function HorizontalScrollSection() {
  const sectionRef = useRef(null)
  const trackRef = useRef(null)

  useEffect(() => {
    const track = trackRef.current
    const totalWidth = track.scrollWidth - window.innerWidth

    gsap.to(track, {
      x: -totalWidth,
      ease: 'none',
      scrollTrigger: {
        trigger: sectionRef.current,
        start: 'top top',
        end: `+=${totalWidth}`,
        scrub: 1,
        pin: true,
        anticipatePin: 1,
      }
    })

    return () => ScrollTrigger.getAll().forEach(t => t.kill())
  }, [])

  return (
    <section ref={sectionRef} className="overflow-hidden">
      <div ref={trackRef} className="flex w-max gap-8 p-16">
        {panels.map((panel, i) => (
          <div key={i} className="w-[80vw] h-screen flex-shrink-0 bg-gray-100 rounded-2xl" />
        ))}
      </div>
    </section>
  )
}

GSAP ScrollTrigger:交错文本揭示(逐行)

useEffect(() => {
  const lines = gsap.utils.toArray('.reveal-line')

  lines.forEach((line, i) => {
    gsap.fromTo(line,
      { y: '100%', opacity: 0 },
      {
        y: '0%',
        opacity: 1,
        duration: 0.8,
        ease: 'power3.out',
        delay: i * 0.1,
        scrollTrigger: {
          trigger: line,
          start: 'top 90%',
          toggleActions: 'play none none none'
        }
      }
    )
  })

  return () => ScrollTrigger.getAll().forEach(t => t.kill())
}, [])

滚动动画的性能规则

  • 仅对 transform 和 opacity 进行动画处理——绝不使用 top、left、width、height
  • 为连续滚动动画的元素添加 will-change: transform
  • 始终在 useEffect 返回中清理 ScrollTrigger 实例
  • 使用 scrub: 1(而不是 scrub: true)实现平滑擦除
  • 在低端安卓设备上测试——如果在那里掉帧,说明发布的产品有问题
  • useInView 中的 margin: '-80px 0px' 可防止动画触发过早

Individual skills in this repo

This repo contains 1 individual skill — each has its own dedicated page.

相关技能