vue 2.0 中使用 css 变量

<template>
<div :style="styleText" class="div1">
  <button @click="handleClick">修改颜色</button>
</div> </template> <script> export default { data () { return { styleText: { '--text-color': 'red' } } },
methods: {
handleClick () {
document.querySelector('.div1').style.setProperty('--text-color', 'blue');
}
},
mounted () {
// 获取 css 属性值
const color = getComputedStyle(document.documentElement).getPropertyValue('--text-color')

}
}

</script>
<style>
.div1 {
color: var(--text-color);
}
</style>

  

posted @ 2021-02-26 16:07  W-it-H-ou-T  阅读(1302)  评论(2编辑  收藏  举报