通过js动态更新css变量

一、vue2:

全局:

复制代码
<template>
  <div>
    <p>当前背景色由 CSS 变量控制</p>
    <button @click="toggleTheme">切换主题</button>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      themeColor: '#42b883' // 响应式数据
    }
  },
  watch: {
    themeColor(newVal) {
      // 更新 :root 上的 CSS 变量
      document.documentElement.style.setProperty('--theme-color', newVal)
    }
  },
  mounted() {
    // 初始化 CSS 变量(确保首次渲染生效)
    this.updateCssVar()
  },
  methods: {
    toggleTheme() {
      this.themeColor = this.themeColor === '#42b883' ? '#e66767' : '#42b883'
    },
    updateCssVar() {
      document.documentElement.style.setProperty('--theme-color', this.themeColor)
    }
  }
}
</script>

<style>
:root {
  --theme-color: #f0f0f0; /* 默认值 */
}

body {
  background-color: var(--theme-color);
  color: white;
  padding: 20px;
  transition: background-color 0.3s;
}
</style>

局部:

复制代码
<template>
  <div ref="box" class="dynamic-box">
    <p>局部 CSS 变量示例</p>
    <button @click="increaseSize">增大尺寸</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      size: 100
    }
  },
  methods: {
    increaseSize() {
      this.size += 20
      this.$nextTick(() => {
        // 确保 DOM 已更新(虽然这里没改模板结构,但保险起见)
        const el = this.$refs.box
        if (el) {
          el.style.setProperty('--box-size', this.size + 'px')
        }
      })
    }
  },
  mounted() {
    // 初始化
    this.$refs.box.style.setProperty('--box-size', this.size + 'px')
  }
}
</script>

<style scoped>
.dynamic-box {
  --box-size: 100px;
  width: var(--box-size);
  height: var(--box-size);
  background: #409eff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: width 0.2s, height 0.2s;
}
</style>

vue3:

全局:

复制代码
<template>
  <div class="box">Hello, CSS Variables!</div>
  <button @click="changeColor">切换颜色</button>
</template>

<script setup>
import { ref, watch } from 'vue'

const bgColor = ref('#42b883')

// 监听 bgColor 变化,并同步到 :root 的 CSS 变量
watch(bgColor, (newColor) => {
  document.documentElement.style.setProperty('--bg-color', newColor)
})

function changeColor() {
  bgColor.value = bgColor.value === '#42b883' ? '#e66767' : '#42b883'
}
</script>

<style>
:root {
  --bg-color: #f0f0f0; /* 默认值 */
}

.box {
  background-color: var(--bg-color);
  padding: 20px;
  color: white;
  transition: background-color 0.3s;
}
</style>

局部:

复制代码
<template>
  <div ref="container" class="container">
    <p>局部 CSS 变量控制</p>
    <button @click="updateRadius">增大圆角</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const container = ref(null)
let radius = 10

function updateRadius() {
  radius += 5
  if (container.value) {
    container.value.style.setProperty('--border-radius', `${radius}px`)
  }
}
</script>

<style scoped>
.container {
  --border-radius: 10px;
  border: 2px solid #ccc;
  border-radius: var(--border-radius);
  padding: 16px;
  transition: border-radius 0.2s;
}
</style>
相关推荐
豹哥学前端3 小时前
事件循环(Event Loop)深度解析:让你彻底搞懂 JS 的执行顺序
前端·javascript·面试
竹林8183 小时前
用 wagmi v2 + Next.js 14 搞 NFT 交易市场前端:从合约调用失败到顺利上架,我踩了哪些坑
javascript·next.js
前端不开发3 小时前
用一个 Bookmarklet(书签脚本),给任意网页挂一个可拖拽悬浮窗
前端·javascript
接着奏乐接着舞3 小时前
【无标题】
开发语言·前端·javascript
biubiubiu_LYQ3 小时前
萌新小白基础篇之CSS定位布局(干货满满)!
css
雨雨雨雨雨别下啦3 小时前
心理健康AI助手 - 项目总结
前端·javascript·vue.js·人工智能·信息可视化
风之舞_yjf3 小时前
Vue基础(32)_TodoList案例
前端·javascript·vue.js
Amos_Web5 小时前
Rspack 源码解析 (2) —— 从 rspack build 到输出 dist,完整编译链路详解
前端·javascript
张元清5 小时前
Ref 逃生舱:用 React Hook 解决闭包陈旧、回调身份不稳和强制更新
前端·javascript·面试
暗冰ཏོ5 小时前
CSS 超详细讲解(从基础到高级实战)
前端·css·css3·sass·scss