动画系统 #

Transition 过渡 #

基本用法 #

jsx
<Box
  bg="blue.500"
  color="white"
  p={4}
  transition="all 0.3s"
  _hover={{ bg: 'blue.600', transform: 'translateY(-2px)' }}
>
  悬停查看过渡效果
</Box>

过渡属性 #

jsx
<Box
  transition="background-color 0.2s ease-in-out"
  _hover={{ bg: 'blue.600' }}
>
  指定过渡属性
</Box>

<Box
  transitionProperty="all"
  transitionDuration="normal"
  transitionTimingFunction="ease"
>
  使用过渡属性
</Box>

Animation 动画 #

关键帧动画 #

jsx
import { keyframes } from '@emotion/react'

const spin = keyframes`
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
`

function Spinner() {
  return (
    <Box
      animation={`${spin} 1s linear infinite`}
      w="40px"
      h="40px"
      bg="blue.500"
    />
  )
}

淡入动画 #

jsx
const fadeIn = keyframes`
  from { opacity: 0; }
  to { opacity: 1; }
`

function FadeInBox() {
  return (
    <Box
      animation={`${fadeIn} 0.5s ease-in`}
      p={4}
      bg="green.500"
    >
      淡入效果
    </Box>
  )
}

Framer Motion 集成 #

Chakra UI 内置了 Framer Motion 支持。

基本动画 #

jsx
import { motion } from 'framer-motion'

const MotionBox = motion(Box)

function AnimatedCard() {
  return (
    <MotionBox
      p={6}
      bg="white"
      borderRadius="lg"
      shadow="md"
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: 0.5 }}
    >
      动画卡片
    </MotionBox>
  )
}

悬停动画 #

jsx
<MotionBox
  p={6}
  bg="blue.500"
  color="white"
  borderRadius="lg"
  whileHover={{ scale: 1.05 }}
  whileTap={{ scale: 0.95 }}
>
  点击查看动画
</MotionBox>

列表动画 #

jsx
const container = {
  hidden: { opacity: 0 },
  show: {
    opacity: 1,
    transition: {
      staggerChildren: 0.1
    }
  }
}

const item = {
  hidden: { opacity: 0, y: 20 },
  show: { opacity: 1, y: 0 }
}

function AnimatedList({ items }) {
  return (
    <MotionBox
      as="ul"
      variants={container}
      initial="hidden"
      animate="show"
    >
      {items.map((item, i) => (
        <MotionBox
          as="li"
          key={i}
          variants={item}
          mb={2}
          p={4}
          bg="gray.100"
          borderRadius="md"
        >
          {item}
        </MotionBox>
      ))}
    </MotionBox>
  )
}

实用示例 #

滑入动画 #

jsx
const slideIn = keyframes`
  from {
    transform: translateX(-100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
`

function SlideInMenu({ isOpen }) {
  return (
    <Box
      position="fixed"
      left={0}
      top={0}
      h="100vh"
      w="250px"
      bg="white"
      shadow="lg"
      animation={isOpen ? `${slideIn} 0.3s ease-out` : undefined}
    >
      菜单内容
    </Box>
  )
}

脉冲动画 #

jsx
const pulse = keyframes`
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
`

function PulseButton() {
  return (
    <Button
      animation={`${pulse} 2s infinite`}
      colorScheme="blue"
    >
      点击我
    </Button>
  )
}

下一步 #

现在你已经掌握了动画系统,接下来学习 国际化,了解如何实现多语言支持!

最后更新:2026-03-28