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

vue3+elementplugs+原生css实现切换主题色

一般写在导航栏或者切换主题菜单内部,这里写在component组件下面的HeaderBar里面

<template><el-dropdown @command="handleThemeChange"><el-button size="small" icon="Refresh" class="theme-switch-btn">切换主题<el-icon class="el-icon--right"><ArrowDown /></el-icon></el-button><template #dropdown><el-dropdown-menu><el-dropdown-item command="light">浅色主题</el-dropdown-item><el-dropdown-item command="dark">深色主题</el-dropdown-item><el-dropdown-item command="blue">蓝色主题</el-dropdown-item><el-dropdown-item command="red">红色主题</el-dropdown-item></el-dropdown-menu></template></el-dropdown>
</template><script setup lang="ts">
import { ArrowDown, Refresh } from '@element-plus/icons-vue'
import { onMounted } from 'vue'// 处理主题切换
const handleThemeChange = (theme: string) => {// 移除所有主题类document.documentElement.classList.remove('theme-light', 'theme-dark', 'theme-blue', 'theme-red')// 添加选中的主题类document.documentElement.classList.add(`theme-${theme}`)// 保存主题偏好localStorage.setItem('app-theme', theme)
}// 初始化主题
onMounted(() => {const savedTheme = localStorage.getItem('app-theme') || 'light'document.documentElement.classList.add(`theme-${savedTheme}`)
})
</script><style scoped>
.theme-switch-btn {margin-right: 10px;
}
</style>

创建styles/theme.css

/* 基础变量 - 浅色主题作为默认主题 */
:root {/* 背景色 */--bg-color: #ffffff;--bg-secondary-color: #f5f7fa;--bg-tertiary-color: #eef2f7;/* 文本色 */--text-primary-color: #1d2129;--text-secondary-color: #4e5969;--text-tertiary-color: #86909c;/* 边框色 */--border-color: #dce1e8;/* 主题色 */--primary-color: #409eff;--primary-light-color: #e6f7ff;--primary-dark-color: #1890ff;/* 功能色 */--success-color: #52c41a;--warning-color: #faad14;--error-color: #ff4d4f;
}/* 深色主题 */
.theme-dark {/* 背景色 */--bg-color: #141414;--bg-secondary-color: #1f1f1f;--bg-tertiary-color: #2c2c2c;/* 文本色 */--text-primary-color: #f5f5f5;--text-secondary-color: #e0e0e0;--text-tertiary-color: #a0a0a0;/* 边框色 */--border-color: #383838;
}/* 蓝色主题 */
.theme-blue {/* 主题色 */--primary-color: #0066cc;--primary-light-color: #e0edff;--primary-dark-color: #0052a3;/* 调整部分背景色配合主题 */--bg-secondary-color: #f0f7ff;
}/* 红色主题 */
.theme-red {/* 主题色 */--primary-color: #cc0000;--primary-light-color: #ffe0e0;--primary-dark-color: #a30000;/* 调整部分背景色配合主题 */--bg-secondary-color: #fff0f0;
}/* 应用主题变量到Element Plus组件 */
:root,
.theme-light,
.theme-dark,
.theme-blue,
.theme-red {/* Element Plus 主题变量覆盖 */--el-color-primary: var(--primary-color);--el-color-primary-light-9: var(--primary-light-color);--el-color-primary-dark-2: var(--primary-dark-color);--el-bg-color: var(--bg-color);--el-bg-color-page: var(--bg-secondary-color);--el-bg-color-overlay: var(--bg-tertiary-color);--el-text-color-primary: var(--text-primary-color);--el-text-color-secondary: var(--text-secondary-color);--el-text-color-placeholder: var(--text-tertiary-color);--el-border-color: var(--border-color);--el-border-color-light: var(--border-color);
}

在main.js里面全局引入

import {createApp} from 'vue'
import App from './App.vue'
import router from "@/router/index.js";
import {createPinia} from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate"
import ElementPlus from "element-plus"
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
// 引入主题样式
import './styles/theme.css'
const app = createApp(App)const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
app.use(pinia)
app.use(router)
app.use(ElementPlus, {locale: zhCn
})app.mount('#app')

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

相关文章:

  • Windows用Notepad++编辑Shell脚本:一招解决Linux执行报错问题
  • 自己做网站要不要租服务器软件开发工程师时薪
  • 工业智能科技网站设计做搜狗pc网站软件下载
  • 企业网站制作收费拍卖行 网站建设
  • 网站建设和网站开发的区别域名主机基地
  • 宁波营销型网站建设首选肥东县建设局网站
  • 工商营业执照网上申报关键词优化报价
  • 帮人做网站要怎么赚钱吗网页设计软件培训机构
  • seo网站建设费用济南网站建设第六网建
  • 昆山市网站建设产品运营主要做什么
  • 网站建设进无形资产的规定电子商务网站硬件建设的核心
  • 免费的毕业设计网站建设北京移动端网站建设
  • 简易网站模板wordpress菜单创建
  • 哪个网站可以做兼职讲师建设部网站施工员查询
  • wordpress 通用代码seo关键词排行优化教程
  • 好看的知名企业网站制作网页的软件
  • 牛商网专注营销型网站建设加速器怎么加速网页
  • 某公司网站源码电脑版商城网站建设
  • dw和vs做网站哪个好用软件开发培训学费
  • 重庆网站建设velpaiseo优化排名易下拉试验
  • 南京做网站公司地点网站建设银川
  • frontpage可以做网站吗网站主色调有几种
  • 做网站用什么域名比较好烟台规划网站
  • 博客网站是自己做的吗wordpress分类自定义title
  • 做视频的网站socks5免费代理地址
  • 深圳做网站最好的公可以推广的平台
  • 购物网站app开发高端网站建设 aspx
  • 湖南省建设工程造价管理协会网站做问卷调查赚钱的网站
  • 网站范例wordpress如何更改页面链接地址
  • 网站多语言解决方案嘉兴企业网站模板建站