当前位置: 首页 > news >正文

Vue + Scss项目中实现自定义颜色主题的动态切换

当时面试的时候遇到面试官问的一个问题如何实现自定义颜色主题切换,当时我做的只是elementUIPlus提供的暗黑和默认主题切换​​​​​​​


theme.scss

// 增加自定义主题类型
$themes: (
  light: (/* 原有配置保持不变 */),
  dark: (/* 原有配置保持不变 */),
  custom: () // 空映射用于自定义主题
);

// 新增CSS变量定义方式
:root {
  // 原有主题变量...
  
  // 自定义主题变量
  --custom-primary-color: #409eff;
  --custom-background-color: #ffffff;
  --custom-text-color: #303133;
}

main.ts

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import { initCustomTheme } from './utils/themeManager' // 新增导入

const app = createApp(App)

app.use(ElementPlus)

// 新增自定义主题初始化
initCustomTheme()
app.mount('#app')

 themeManager.ts

// 新增自定义主题设置方法
export const setCustomTheme = (colors: {
  primary: string
  background: string
  text: string
}) => {
  // 更新CSS变量
  document.documentElement.style.setProperty('--custom-primary-color', colors.primary)
  document.documentElement.style.setProperty('--custom-background-color', colors.background)
  document.documentElement.style.setProperty('--custom-text-color', colors.text)
  
  // 应用自定义主题
  document.documentElement.setAttribute('data-theme', 'custom')
  ElConfigProvider.setGlobalConfig({
    theme: {
      primary: colors.primary
    }
  })
  
  // 存储自定义配置
  localStorage.setItem('customTheme', JSON.stringify(colors))
}

// 初始化自定义主题
export const initCustomTheme = () => {
  const saved = localStorage.getItem('customTheme')
  if (saved) {
    const colors = JSON.parse(saved)
    setCustomTheme(colors)
  }
}

ThemeCustomizer.vue

<template>
  <div class="color-picker">
    <el-color-picker v-model="colors.primary" show-alpha @change="updateTheme" />
    <el-color-picker v-model="colors.background" @change="updateTheme" />
    <el-color-picker v-model="colors.text" @change="updateTheme" />
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { setCustomTheme } from '../utils/themeManager'

const colors = reactive({
  primary: '#409eff',
  background: '#ffffff',
  text: '#303133'
})

const updateTheme = () => {
  setCustomTheme({ 
    primary: colors.primary,
    background: colors.background,
    text: colors.text
  })
}
</script>

<style lang="scss" scoped>
.color-picker {
  @include theme {
    background: theme-value(background-color);
    padding: 20px;
    border: 1px solid theme-value(primary-color);
  }
  
  :deep(.el-color-picker__trigger) {
    @include theme {
      border-color: theme-value(primary-color);
    }
  }
}
</style>

http://www.dtcms.com/a/108427.html

相关文章:

  • 深入解析Translog机制:Elasticsearch的数据守护者
  • MySQL 服务基础介绍
  • 第二十章:Python-Matplotlib库实现函数可视化
  • 日本IT|浅谈intramart现状及分析
  • 玛卡巴卡的k8s知识点问答题(六)
  • QT软件设计可考虑回答
  • GFS论文阅读笔记
  • 《二叉树:二叉树的顺序结构->堆》
  • Linux基础命令:开启系统操作之旅
  • 将一个新的机器人模型导入最新版isaacLab进行训练(以unitree H1_2为例)
  • 2025最新WordPress网站被挂码的原因与解决方案
  • 透视投影(Perspective projection)与等距圆柱投影(Equirectangular projection)
  • 学习笔记 - Flask - 02
  • Sam Altman 表示 OpenAI 的性能问题将导致产品延迟
  • [Linux]从硬件到软件理解操作系统
  • 学习笔记—数据结构—二叉树(算法题)
  • fyrox 2D和3D游戏的制作
  • 【云计算物理网络】数据中心网络架构设计
  • 蓝桥杯备考:完全二叉树的节点个数
  • mysql and redis简化版
  • 【Easylive】视频在线人数统计系统实现详解 WebSocket 及其在在线人数统计中的应用
  • 蓝桥杯DFS算法题(java)
  • RainbowDash 的旅行
  • HCIP的配置
  • 使用Python和OpenCV进行指纹识别与验证
  • 通过必应壁纸官方api实现网页背景图片随机展示
  • substring() 和 slice() 这两个方法的相同与不同
  • Python 脚本:自动化你的日常任务
  • 【element ui】翻页记忆功能、多选时执行删除操作,刷新表格问题
  • VS+Qt配置QtXlsx库实现execl文件导入导出(全教程)