vue2 scss主题切换

vue2 scss主题切换

1.定义scss全局变量

//@/assets/css/theme.scss
$primaryColor: var(--primaryColor);
$primaryTextColor: var(--primaryTextColor);

2.定义样式表

//@/lib/theme.scss
export default {
  default: {
    primaryColor: "#000",
    primaryTextColor: "green",
  },
  my: {
    primaryColor: "skyblue",
    primaryTextColor: "yellow",
  },
};

3.新增主题模块

import themes from "@/lib/theme";

//改变样式
const changeStyle = (themeObj) => {
  Object.keys(themeObj).forEach((item) => {
    document
      .getElementsByTagName("body")[0]
      .style.setProperty(`--${item}`, themeObj[item]);
  });
};
//设置主题
export const setTheme = (themeName) => {
  localStorage.setItem("theme", themeName);
  const themeConfig = themes[themeName];
  changeStyle(themeConfig);
};
//初始化
export const initTheme = () => {
  const themeName = localStorage.getItem("theme");
  if (themeName) {
    setTheme(themeName);
  } else {
    setTheme("default");
  }
};

4.使用

//main.js
import Vue from "vue";
import App from "./App.vue";
import { initTheme } from "@/module/theme";

Vue.config.productionTip = false;

new Vue({
  render: (h) => h(App),
}).$mount("#app");

//初始化样式
initTheme();
//test.vue
<template>
  <div>
    <div class="box">你好,世界</div>
    <button @click="setTheme('default')">默认样式</button>
    <button @click="setTheme('my')">我的样式</button>
  </div>
</template>

<script>
import { setTheme } from "@/module/theme";
export default {
  methods: {
    setTheme(themeName) {
      setTheme(themeName);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/assets/css/theme.scss";
.box {
  width: 200px;
  height: 200px;
  color: $primaryTextColor;
  background-color: $primaryColor;
}
</style>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
点击按钮切换scss变量react是指在React项目中,通过点击按钮来切换应用中使用的scss变量。 在React中,我们可以使用state来保存和管理页面中的状态。所以,我们可以创建一个按钮组件,通过点击按钮来改变其中的scss变量。 首先,在项目中引入所需的scss文件,并创建一个变量文件来保存需要切换的scss变量。 然后,创建一个按钮组件,并在其中定义一个状态变量来保存当前的scss变量: ``` import React, { useState } from "react"; import "./styles.scss"; const Button = () => { const [theme, setTheme] = useState("default"); const handleThemeChange = () => { // 根据当前的主题切换到另一个主题 if (theme === "default") { setTheme("alternative"); } else { setTheme("default"); } }; return ( <button onClick={handleThemeChange} className={theme}> 切换主题 </button> ); }; export default Button; ``` 在上面的例子中,我们创建了一个按钮组件,使用useState来定义了一个名为theme的状态变量,默认值为"default"。在handleThemeChange函数中,我们通过判断当前的theme值来切换到另一个主题。然后,在render函数中,我们将这个theme值作为classname应用到按钮上。 最后,在项目的主文件中引入刚刚创建的按钮组件: ``` import React from "react"; import Button from "./Button"; const App = () => { return ( <div> <h1>点击按钮切换scss变量React</h1> <Button /> </div> ); }; export default App; ``` 上述代码中,我们创建了一个App组件,并在其中使用了刚刚创建的按钮组件Button。 这样,当我们点击按钮时,按钮组件的状态会发生改变,从而切换到另一个scss变量,并且页面中使用该变量的样式会跟着改变

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值