当前位置:首页 > VUE

vue主题色实现

2026-01-19 07:00:31VUE

实现 Vue 主题色的方法

使用 CSS 变量动态切换主题色

src/assets/styles 目录下创建 theme.css 文件,定义主题色变量:

:root {
  --primary-color: #409EFF;
  --secondary-color: #67C23A;
}
.dark-theme {
  --primary-color: #304156;
  --secondary-color: #4F88C6;
}

main.js 中导入样式文件:

import '@/assets/styles/theme.css'

组件中使用 CSS 变量:

<template>
  <button :style="{ backgroundColor: 'var(--primary-color)' }">按钮</button>
</template>

切换主题的 JavaScript 代码:

function toggleTheme() {
  document.documentElement.classList.toggle('dark-theme')
}

使用 Vuex 管理主题状态

安装必要依赖:

npm install vuex

创建 store/modules/theme.js

const state = {
  currentTheme: 'light'
}

const mutations = {
  SET_THEME(state, theme) {
    state.currentTheme = theme
  }
}

export default {
  namespaced: true,
  state,
  mutations
}

在组件中切换主题:

methods: {
  changeTheme() {
    const newTheme = this.$store.state.theme.currentTheme === 'light' ? 'dark' : 'light'
    this.$store.commit('theme/SET_THEME', newTheme)
    document.documentElement.className = newTheme + '-theme'
  }
}

使用 SCSS 变量和混合

创建 src/assets/styles/variables.scss

$themes: (
  light: (
    primary-color: #409EFF,
    secondary-color: #67C23A
  ),
  dark: (
    primary-color: #304156,
    secondary-color: #4F88C6
  )
);

创建 src/assets/styles/mixins.scss

@mixin theme-aware($key, $color) {
  @each $theme-name, $theme-color in $themes {
    .#{$theme-name}-theme & {
      #{$key}: map-get($theme-color, $color)
    }
  }
}

组件中使用:

.button {
  @include theme-aware('background-color', 'primary-color');
}

使用第三方库

安装 vue-theme-switcher

npm install vue-theme-switcher

基本使用:

import Vue from 'vue'
import ThemeSwitcher from 'vue-theme-switcher'

Vue.use(ThemeSwitcher, {
  themes: {
    light: {
      primary: '#409EFF'
    },
    dark: {
      primary: '#304156'
    }
  }
})

在组件中切换:

this.$theme.set('dark')

响应式主题切换实现

创建主题监听器:

const prefersDark = window.matchMedia('(prefers-color-scheme: dark)')
prefersDark.addListener(e => {
  const theme = e.matches ? 'dark' : 'light'
  this.$store.commit('theme/SET_THEME', theme)
})

持久化主题选择:

vue主题色实现

// 保存到localStorage
localStorage.setItem('user-theme', theme)

// 初始化时读取
const savedTheme = localStorage.getItem('user-theme') || 'light'
this.$store.commit('theme/SET_THEME', savedTheme)

标签: 主题vue
分享给朋友:

相关文章

vue 实现全选

vue 实现全选

Vue 实现全选功能 在 Vue 中实现全选功能通常需要结合复选框的状态管理,以下是几种常见的实现方式: 使用 v-model 绑定数组 通过 v-model 绑定一个数组来管理选中的项,全选时将该…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…