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>


相关推荐
姜鱼问生4 小时前
Nginx 缓存命中率监控:从 X-Cache-Status 到实时统计
运维·nginx·缓存
Apipi*5 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
天启HTTP6 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
运维行者_6 小时前
网络性能监控怎么做?从自动发现到根因分析的4个环节
运维·服务器·网络·人工智能·支持向量机
此时不提桶,更待何时7 小时前
06-14-A-Kafka集群运维与迁移实战详解
运维·kafka
hz567898 小时前
涉密视频会议设备配置指南:终端、音视频采集与配套设施选型
服务器·网络·数据库·安全·实时音视频·信息与通信·智能硬件
广州浮点FLOATLIC8 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
优橙教育8 小时前
零基础学AI应用开发要多久?3个月能到什么水平
服务器·开发语言·网络·php
IT研究所9 小时前
AI-ITR平台如何减少客户问题反复升级?
大数据·运维·人工智能·低代码·自然语言处理·安全架构·企微
BLUcoding9 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom