我想在Vue组件中访问项目中css根变量的值。例如,改变10个变量,包括颜色、页边距和字体大小,按一个按钮到新的值,然后按同样的按钮改变变量到它们的(默认)原始值,事实上改变了项目中的css根变量的值。我怎样才能做到这一点呢?事实上,我想通过按一个按钮来切换黑暗和光明。
这个想法的灵感来自于下面链接中的变化,链接中的例子是用纯JavaScript脚本写的,我想在Next Js Framework上开发的Vue项目中使用它。实现一个网站的10个左右的变量,其值必须立即改变,按一个按钮,在暗光模式下切换。
如何访问和更改Css根变量?
new Vue({
el: "#theme",
data: {
return {
dark: true,
};
},
watch: {
dark() {
let bg = this.dark ? "#1b1b1b" : "#f5f5f5";
let txtColor = this.dark ? "#999999" : "#333333";
document.documentElement.style.setProperty("--bg", bg);
document.documentElement.style.setProperty("--txt", txtColor);
}
}
});
:root{
--bg: white;
--txt: black;
}
body {
background-color: var(--bg);
color: var(--txt)
}
article {
padding: 50px
}
article h2 {
margin-top: 100px;
}
<div id="theme">
<button @click="dark=!dark">dark</button>
<article>
<h1>Hello World</h1>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean v
</article>
</div>
你在以下地方有一个语法错误 data
但除此之外,它工作正常。你是想在页面加载后立即运行吗?
new Vue({
el: "#theme",
data() {
return {
dark: false,
root: null
};
},
mounted: function() {
this.root = document.documentElement;
},
watch: {
dark: {
handler: function() {
// because we are using this handler immideatly we need to wait for data changes using nextTick.
this.$nextTick(() => {
if (this.dark) {
this.root.style.setProperty("--bg", "red");
this.root.style.setProperty("--text", "black");
this.root.style.setProperty("--padding", "10px");
this.root.style.setProperty("--font", "1rem");
} else {
this.root.style.setProperty("--bg", "blue");
this.root.style.setProperty("--text", "green");
this.root.style.setProperty("--padding", "15px");
this.root.style.setProperty("--font", "2rem");
}
})
},
immediate: true
}
}
});
:root {
--bg: white;
--bg-text: black;
--padding: 5px;
--font: 3rem;
}
body {
background-color: var(--bg);
color: var(--bg-text)
}
article {
padding: 50px
}
article h2 {
margin-top: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="theme">
<button @click="dark=!dark">dark</button>
<article>
<h1>Hello World</h1>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean v
</article>
</div>