<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
:root {
--THEME: var(--USER-THEME, red);
--THEME-COLOR: var(--USER-THEME-COLOR, green);
}
#main {
width: 300px;
height: 300px;
background: var(--THEME);
display: flex;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.con {
width: 100px;
height: 100px;
background: var(--THEME-COLOR);
}
</style>
</head>
<body>
<div id="main">
<div class="con"></div>
</div>
</body>
<script type="text/javascript">
document.getElementById("main").onclick = function() {
const elm = document.documentElement
const colorArr = ['blue', 'black']
elm.style.setProperty('--USER-THEME', colorArr[0]);
elm.style.setProperty('--USER-THEME-COLOR', colorArr[1]);
}
</script>
</html>
复制代码
CSS自定义属性切换主题
最新推荐文章于 2024-05-28 16:26:24 发布