CSS_scss切换主题

目录assets/theme以下新建文件

_handle.scss

@import './_themes.scss';

// 定义混合指令, 切换主题,并将主题中的所有规则添加到theme-map中
@mixin themify() {

    // 将themes中规则放入theme-map
    @each $theme-name,
    $theme-map in $themes {
        $theme-map: $theme-map  !global;

        [data-theme="#{$theme-name}"] & {
            // 表示包含下面函数 themed(), 类似于插槽
            @content;
            $theme-map : null !global;
        }
    }
}

@function themed($key) {
    @return map-get($theme-map, $key);
}

_themes.scss

$themes: (
light:(main_background: #fff,
    text_color: #333,
    text_color1: #868C9A,
    text_color2: #868C9A,
    text_color3: #000000, //
    tab_background: #F5F5F5,
    tab_background1: #fff, //
    tab_background2: #868C9A, //
    active_line: #00D6CA,
    footer_background: #fff,
    btn_main: #00D6CA,
    color_main: #00D6CA,
    btn_background: #00D6CA, //按钮颜色
    btn_background1: #eff7ff, //按钮颜色
    btn_background2: #C8CAD2, //按钮颜色
    input_background: #F5F5F5,
    cont_background: #f6f6f6,
),
dark:(main_background: #121212, //主题色
    text_color: #fff, //文字颜色
    text_color1: #868D9A, //文字浅色
    text_color2: #fff, // 资金页面所用对应
    text_color3: #fff, //
    tab_background: #242424, //tab切换背景颜色
    tab_background1: #000, //tab切换背景颜色
    tab_background2: #242424, //tab切换背景颜色
    active_line: #00D6CA, //tab选中样式颜色
    footer_background: #1a1a1a, // 底部颜色
    btn_main: #00D6CA, //主题色按钮
    color_main: #00D6CA, //主题色文字
    btn_background: #474B62, //按钮颜色
    btn_background1: #112542, //按钮颜色
    btn_background2: #1B2134, //按钮颜色
    input_background: #242424, //input背景颜色
    cont_background: #242424, //title背景色
))

index.scss

@import './_handle.scss';

.mainBackground {
    @include themify() {
        background: themed("main_background") !important;
    }
}

.textColor {
    @include themify() {
        color: themed("text_color") !important;
    }
}

修改vue.config.js文件

const scss_path = `@/assets/theme/index.scss`

module.exports = defineConfig({
	css: {
		loaderOptions: {
			scss: {
				additionalData: `@import "${scss_path}";`
			},
		}
	},
})

app.vue设置默认主题

mounted() {
	window.document.documentElement.setAttribute('data-theme', '替换本地存储的theme')
},

状态管理设置点击设置主题

// 获取localStorage
const getStorage = function (key) {
    const str = window.localStorage.getItem(key)
    if (!str) {
        return null
    }
    return JSON.parse(str)
}

// 移除localStorage
const removeStorage = function (key) {
    window.localStorage.removeItem(key)
}
// 设置sessionStorage
const setSessionStorage = function (key, obj) {
    let json = JSON.stringify(obj)
    window.sessionStorage.setItem(key, json)
    // console.log('设置语言', key, json)
}

const changeTheme = (theme) => {
    if ((navigator.userAgent).indexOf('Html5Plus') > -1) {
        if (theme == 'light') {
            plus.navigator.setStatusBarBackground('#ffffff');
            plus.navigator.setStatusBarStyle('dark'); // 只能是dark 和 light
        } else {
            plus.navigator.setStatusBarBackground('#121212');
            plus.navigator.setStatusBarStyle('light'); // 只能是dark 和 light
        }
    } else {
        let meta = document.querySelector('meta[name="theme-color"]');
        if (theme == 'light') {
            meta.setAttribute('content', '#fff');
        } else {
            meta.setAttribute('content', '#121212');
        }
    }
}

export default {
	state: {
	    theme: getStorage('theme') || 'light'
	},
	getters: {
	    theme: state => state.theme
	},
	mutations: {
    SET_THEME: (state, theme) => {
		state.theme = theme
		window.document.documentElement.setAttribute('data-theme', theme)
		changeTheme(theme)
		setStorage('theme', theme)
    },
}

使用

import { mapGetters, mapMutations } from "vuex";

export default {
    data() {
        return {
        }
    },
    computed: {
        ...mapGetters(['theme'])
    },
    methods: {
        ...mapMutations('SET_THEME'),
        changeModel(type) {
            this.type = type;
            this.SET_THEME(type)
        },
        /* changeModel() {
            let type = ''
            if (this.theme == 'light') {
                type = 'dark'
            } else {
                type = 'light'
            }
            this.SET_THEME(type)
        }, */
    }
}
### CSSSCSS 的主要区别及使用场景 #### 功能对比 CSS 是一种用于描述 HTML 文档外观和格式的样式语言,它本身功能有限,不支持变量、嵌套规则或函数等高级特性。相比之下,SCSS 是一种 CSS 预处理器,提供了许多增强功能,使得样式编写更加高效和可维护[^2]。 1. **变量** SCSS 支持变量定义和使用,例如 `$primary-color: #fffff;`,这使得开发者可以在整个文件中重复使用这些变量,方便管理和修改。而 CSS 原生并不支持变量,尽管现代浏览器支持 CSS 自定义属性(如 `--primary-color`),但它们的功能仍不如 SCSS 变量强大[^3]。 2. **嵌套规则** SCSS 允许选择器嵌套,可以更好地反映 HTML 结构并减少重复代码。例如: ```scss .container { color: black; a { text-decoration: none; } } ``` 而在 CSS 中,每个选择器必须独立书写,无法实现嵌套效果[^4]。 3. **混合器(Mixins)** SCSS 提供了混合功能,可以创建可重用的样式块,甚至支持带有参数的样式模板。例如: ```scss @mixin border-radius($radius) { border-radius: $radius; } .box { @include border-radius(10px); } ``` 这种功能在 CSS 中是不可用的,需要通过手动复制粘贴来实现类似效果[^3]。 4. **运算符和函数** SCSS 允许在样式中执行简单的算术计算和颜色函数等操作。例如: ```scss $primary-color: blue; h1 { color: darken($primary-color, 10%); } ``` 而 CSS 不支持这些功能,所有的计算都需要在运行时通过 JavaScript 或其他工具完成[^4]。 #### 可维护性 CSS 的代码结构较为简单,但在复杂项目中容易变得混乱且难以维护。SCSS 通过引入变量、嵌套规则和混合器等功能,显著提高了代码的清晰度和可维护性。此外,SCSS 文件需要编译为普通的 CSS 文件后才能被浏览器解析,这意味着开发者可以在构建过程中优化最终的输出代码[^2]。 #### 性能与兼容性 CSS 的性能通常较好,因为它直接由浏览器解析,而 SCSS 文件需要经过编译步骤,可能会略微增加开发流程中的复杂性。然而,这种编译过程不会对最终用户的体验产生明显影响。至于浏览器兼容性,SCSS 在编译阶段会将所有高级特性转换为普通 CSS 代码,因此不受浏览器限制。 #### 使用场景 - **CSS**:适用于简单的网页样式设计,或者需要在运行时动态修改样式的场景(如主题切换、响应式布局)。CSS 的灵活性使其成为这类需求的理想选择[^2]。 - **SCSS**:适用于复杂的样式设计和预处理器开发。当项目规模较大、样式需求复杂时,SCSS 的高级特性可以帮助开发者更高效地管理代码。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值