Vue3中样式变量的使用

css 复制代码
//'./assets/styles/variables.module.scss'

$headerHeight: 140px;
$footerHeight: 200px;

:export {
  headerHeight: $headerHeight;
  footerHeight: $footerHeight;
}

:root {
  --headerHeight: #{$headerHeight};
  --footerHeight: #{$footerHeight};
  --customProperty: 我是自定义属性在 root 中定义;
}
javascript 复制代码
import { createApp } from 'vue'

import './assets/styles/variables.module.scss'

import App from './App.vue'

createApp(App).mount('#app')
javascript 复制代码
<script setup>
import {computed, ref} from "vue";
import variables from '@/assets/styles/variables.module.scss'

//场景1 通过xxx.module.scss定义样式变量 js中通过variables.xxx获取
console.log('🍎', variables, 'headerHeight=' + variables?.headerHeight);

//场景2 通过getComputedStyle(DOMElement).getPropertyValue方法获取:root下定义的样式变量
console.log('🍊', getComputedStyle(document.documentElement)
.getPropertyValue('--customProperty')
);

const isWhite = ref(true);

const getFontColor = computed(() => {
  return isWhite.value ? 'white' : 'black'
})

const changeFontColor = () => {
  isWhite.value = !isWhite.value
}
</script>

<template>
  <div class="test-style">
    Hello world!
  </div>
  <button @click="changeFontColor">change Font Color</button>
</template>

<style scoped lang="sass">
.test-style
  //场景3 在style标签中获取root中定义的变量
  height: calc(50svh - var(--headerHeight))
  //场景4 通过v-bind结合vue的computed函数动态获取(常用于dark/light切换场景)
  color: v-bind(getFontColor)
  display: flex
  justify-content: center
  align-items: center
  font-size: 200px
  font-weight: bold
  background-color: #a2dc04
  text-shadow: #fff7f4 -8px 0px 5px
</style>


相关推荐
sxgzzn5 小时前
光伏数智化综合平台:让光伏电站运维更智能、更高效
运维
fengyehongWorld5 小时前
TeraTerm ttl脚本登录wsl
linux·teraterm
乌托邦的逃亡者6 小时前
Linux中如何检测IP冲突
linux·运维·tcp/ip
一曦的后花园6 小时前
linux搭建promethes并对接node-exporter指标
linux·运维·服务器
乌托邦的逃亡者7 小时前
CentOS/Openeuler主机中,为一个网卡设置多个IP地址
linux·运维·网络·tcp/ip·centos
拾贰_C7 小时前
【OpenClaw | openai | QQ】 配置QQ qot机器人
运维·人工智能·ubuntu·面试·prompt
桌面运维家7 小时前
服务器进程异常监控:快速定位与排障实战指南
运维·服务器
@CLoudbays_Martin117 小时前
UniApp是否能够接入SDK游戏盾呢?
服务器·网络·网络协议·tcp/ip·安全
念恒123067 小时前
进程控制---自定义Shell
linux·c语言
风曦Kisaki8 小时前
# Linux Shell 编程入门 Day02:条件测试、if 判断、循环与随机数
linux·运维·chrome