vue3全局更改颜色字体大小
1.引入插件,js中修改css,安装vueUse
1 | npm i @vueuse/core |
(vueuse/core,VueUse是一款基于组合式API的函数集合,https://vueusejs.com/guide/)
2.定义全局css变量,在app.vue中
1 2 3 4 | :root { --size: 14px; --color:black; } |
3.组件中
<template> <div class="loginContainer"> <el-button plain type="text" style="position:absolute;left:20px;top:20px;font-size:18px;" @click="back">返回</el-button> <div class="box"> <div class="btnBox">按钮</div> <div style="margin-bottom:20px;"> <button @click="change(36)">大</button> <button @click="change(24)">中</button> <button @click="change(14)">小</button> </div> <div> <button @click="changeColor('red')">红</button> <button @click="changeColor('blue')">蓝</button> <button @click="changeColor('yellow')">黄</button> </div> </div> </div> </template> <!-- 全局更改颜色字体大小 --> <script lang="ts" setup> import { ref } from 'vue' import { useCssVar } from '@vueuse/core' const back = () => { window.history.back() } const change = (str: number) => { const sizeValue = useCssVar('--size') sizeValue.value = `${str}px` } const changeColor = (str: string) => { const colorValue = useCssVar('--color') colorValue.value = str } </script> <style lang="scss" scoped> .loginContainer { height: 100vh; width: 100vw; padding: 60px; box-sizing: border-box; cursor: pointer; } .box { margin-top: 30px; } .btnBox{ height: 50px; color: #fff;; text-align: center; line-height: 50px; font-size: var(--size); background:var(--color); margin-bottom:20px; } .itemBox { display: flex; flex-direction: row; margin: 20px auto; } </style>
代码地址:https://gitee.com/yuexiayunsheng/vue3learn/commit/ce66333f79f1e2dc783f20f784d871129d2506ce
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)