1.添加theme样式文件
文件内容如下:
html[data-theme="light"]{
--text-color: #000000;
/* 写需要切换的样式 */
}
html[data-theme="dark"]{
--text-color: #ffffff;
/* 写需要切换的样式 */
}
2.引入样式文件
在main.js中引入文件:
import './styles/theme.css'
3.使用变量设置css样式
使用var(自定义的变量名)来设置动态的css样式
.text {
color: var(--text-color);
}
4.设置主题样式
在index.html文件里设置默认样式
5.切换方法
<button @click="changeTheme">切换</button>
const theme = ref('dark')
const changeTheme = () =>{
document.documentElement.setAttribute("data-theme", theme.value == "dark" ? "light" : "dark")
theme.value = theme.value == 'dark' ? 'light' : 'dark'
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » vue3实现自定义主题色切换功能
发表评论 取消回复