首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >在主题切换示例中添加样式切换的动画效果

在主题切换示例中添加样式切换的动画效果

原创
作者头像
小焱
发布2025-08-27 18:20:14
发布2025-08-27 18:20:14
2360
举报
文章被收录于专栏:前端开发前端开发

下面是在主题切换示例中添加样式切换动画的实现,通过 CSS 过渡(transition)和动态类名实现平滑的主题切换效果:

代码语言:jsx
复制
import { createContext, useContext, useState, useMemo } from 'react';
import './ThemeExample.css'; // 引入样式文件

// 1. 创建主题Context
const ThemeContext = createContext({
  theme: 'light',
  toggleTheme: () => {}
});

// 2. 主题Provider组件
export function ThemeProvider({ children }) {
  const [theme, setTheme] = useState('light');

  // 切换主题方法
  const toggleTheme = () => {
    setTheme(prev => prev === 'light' ? 'dark' : 'light');
  };

  // 缓存主题数据,避免不必要的重渲染
  const value = useMemo(() => ({
    theme,
    toggleTheme
  }), [theme]);

  return (
    <ThemeContext.Provider value={value}>
      {/* 给根元素添加主题类名,用于全局样式切换 */}
      <div className={`theme-container theme-${theme}`}>
        {children}
      </div>
    </ThemeContext.Provider>
  );
}

// 3. 自定义Hook
export function useTheme() {
  const context = useContext(ThemeContext);
  if (!context) {
    throw new Error('useTheme must be used within a ThemeProvider');
  }
  return context;
}

// 4. 主题切换按钮组件
function ThemeToggleButton() {
  const { theme, toggleTheme } = useTheme();
  return (
    <button className="theme-toggle-btn" onClick={toggleTheme}>
      当前主题:{theme === 'light' ? '浅色' : '深色'}(点击切换)
    </button>
  );
}

// 5. 示例内容组件(展示动画效果)
function ThemeContent() {
  return (
    <div className="theme-content">
      <h2>主题切换动画示例</h2>
      <p>这是一段测试文本,会随主题变化颜色</p>
      <div className="theme-card">
        <p>卡片内容</p>
      </div>
    </div>
  );
}

// 6. 根组件
function App() {
  return (
    <ThemeProvider>
      <div className="app">
        <ThemeToggleButton />
        <ThemeContent />
      </div>
    </ThemeProvider>
  );
}

export default App;

配套的 CSS 文件(ThemeExample.css):

代码语言:css
复制
/* 基础样式 */
.theme-container {
  min-height: 100vh;
  transition: background-color 0.5s ease, color 0.5s ease;
  padding: 20px;
}

/* 浅色主题 */
.theme-light {
  background-color: #ffffff;
  color: #333333;
}

/* 深色主题 */
.theme-dark {
  background-color: #2c3e50;
  color: #ecf0f1;
}

/* 按钮样式 */
.theme-toggle-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.3s ease; /* 按钮自身的过渡动画 */
}

/* 浅色主题下的按钮 */
.theme-light .theme-toggle-btn {
  background-color: #3498db;
  color: white;
}

/* 深色主题下的按钮 */
.theme-dark .theme-toggle-btn {
  background-color: #e74c3c;
  color: white;
}

/* 按钮悬停效果 */
.theme-toggle-btn:hover {
  transform: scale(1.05);
}

/* 内容区域样式 */
.theme-content {
  margin-top: 30px;
  transition: all 0.5s ease; /* 内容区域的过渡动画 */
}

/* 卡片样式 */
.theme-card {
  margin-top: 20px;
  padding: 20px;
  border-radius: 8px;
  transition: all 0.5s ease; /* 卡片的过渡动画 */
}

/* 浅色主题下的卡片 */
.theme-light .theme-card {
  background-color: #f8f9fa;
  border: 1px solid #dee2e6;
}

/* 深色主题下的卡片 */
.theme-dark .theme-card {
  background-color: #34495e;
  border: 1px solid #2c3e50;
}

/* 标题动画 */
.theme-content h2 {
  transition: transform 0.5s ease, color 0.5s ease;
}

.theme-dark .theme-content h2 {
  transform: translateY(5px);
}

动画效果说明

  1. 全局过渡
    • 在根容器(theme-container)上添加了 background-colorcolor 的过渡动画(0.5秒),实现背景色和文字色的平滑切换
    • 所有需要动画的元素都添加了 transition 属性,确保样式变化时的连贯性
  2. 按钮动画
    • 按钮颜色随主题切换平滑过渡
    • 添加了悬停时的缩放效果(transform: scale(1.05)),增强交互感
  3. 卡片动画
    • 卡片的背景色、边框色随主题平滑过渡
    • 配合全局动画形成统一的视觉体验
  4. 标题动画
    • 深色主题下标题会有微小的位移(translateY(5px)),增加层次感
    • 颜色变化也添加了过渡效果

实现关键点

  1. 通过类名控制主题:根容器添加 theme-lighttheme-dark 类名,所有子元素通过后代选择器适配不同主题
  2. 统一过渡时间:将大部分动画的过渡时间设置为 0.5s,保持节奏一致
  3. 使用 transition 而非 animation:对于样式切换场景,transition 更简洁,适合处理"从A状态到B状态"的变化
  4. 性能优化:使用 useMemo 缓存主题数据,避免 Provider 重新渲染时不必要的子组件更新

这种实现方式既保证了动画的流畅性,又保持了代码的可维护性,适合在实际项目中使用。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 动画效果说明
  • 实现关键点
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档