vue 实现切换主题色(低配版)

18 篇文章 0 订阅
5 篇文章 0 订阅

切换主题功能,本文采取 CSS自定义变量 实现

  • 使用 document.body.style.setProperty(‘–bg’, ‘red’); 来设置变量

  • 使用 document.body.style.getPropertyValue(‘–bg’); 来获取变量

  • 使用 document.body.style.removeProperty(‘–bg’);来删除变量

  • var()来访问。第二个值表示变量的默认值。如:var(–bg,‘#fff’)

  • :root {}声明变量,变量名前需用 – 开头

关键代码

<template>
  <div id="content">
    <button @click="handleTheme(1)">金色主题</button>
    <button @click="handleTheme(2)">蓝色主题</button>
    <button @click="handleTheme(3)">带背景图主题</button>
    <div>
      我是一段描述
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    return {};
  },
  methods: {
    handleTheme(target) {
      if (target === 1) {
        document.documentElement.style.setProperty("--a", "gold");
        document.documentElement.style.setProperty("--b", "#000");
        document.documentElement.style.setProperty("--c", "12px");
      }
      if (target === 2) {
        document.documentElement.style.setProperty("--a", "blue");
        document.documentElement.style.setProperty("--b", "#555");
        document.documentElement.style.setProperty("--c", "18px");
      }
      if (target === 3) {
        document.documentElement.style.setProperty(
          "--a",
          `url(${require("@/assets/logo.png")})`
        );
        document.documentElement.style.setProperty("--b", "red");
        document.documentElement.style.setProperty("--c", "24px");
      }
    },
  },
};
</script>
 
<style lang="less" scoped>
@import url("../../index.less");
</style>

index.less代码

:root {
    --a: red;
    --b: yellow;
    --c: 16px;
}

#content {
    background: var(--a);
    height: 500px;
    width: 400px;
    font-size: var(--c);
    color: var(--b);
}

实现效果
请添加图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue实现一套切换系统主题的代码,可以按照以下步骤: 1. 定义主题样式表的类名 在实现主题切换时,通常需要用到多个不同的样式表。为了方便统一管理这些样式表,需要为每一个样式表定义一个唯一的类名。可以通过定义一些全局变量或者配置文件来存储这些类名,例如: ```javascript // 全局变量 const themeClasses = { 'dark': 'theme-dark', 'light': 'theme-light' } // 配置文件 export const themeClasses = { 'dark': 'theme-dark', 'light': 'theme-light' } ``` 2. 定义全局样式表 在Vue的全局样式表(通常为`App.vue`组件中的`<style>`标签)中,可以使用变量的方式定义系统主题的样式。例如: ```css /* 定义变量 */ :root { --primary-color: #428bca; --secondary-color: #5cb85c; } /* 定义主题样式 */ .theme-dark { --primary-color: #555555; --secondary-color: #CCCCCC; } .theme-light { --primary-color: #EEEEEE; --secondary-color: #00aacc; } /* 使用变量 */ .button { color: var(--primary-color); background-color: var(--secondary-color); } ``` 3. 切换主题实现主题切换时,通常需要监听一个切换按钮的点击事件,并在事件回调函数中修改全局样式表中的`<style>`标签。可以使用`document.documentElement`来获取`<style>`标签,并在其`innerHTML`属性中修改主题样式。例如: ```javascript // 切换按钮的点击事件 toggleTheme() { // 判断当前主题,并计算下一个主题 const currentTheme = this.theme; const nextTheme = currentTheme === 'dark' ? 'light' : 'dark'; // 获取全局样式表并修改样式 const style = document.documentElement.querySelector('style'); style.innerHTML = style.innerHTML.replace(currentTheme, nextTheme); // 更新当前主题 this.theme = nextTheme; } ``` 通过以上步骤,便可以实现一套简单的主题切换系统。需要注意的是,如果主题样式较多或者样式表较大时,切换主题可能会造成性能问题。可以考虑使用CSS变量或者动态生成样式表来提高性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值