<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
[data-theme='light'] {
/* 背景色 */
--bg-color: #fff;
/*文字颜色 */
--text-color: rgb(47, 102, 180);
}
[data-theme='dark'] {
--bg-color: rgb(63, 55, 55);
--text-color: #fff;
}
.box {
width: 500px;
height: 500px;
background-color: var(--bg-color);
border: 1px solid var(--active-color);
}
.title {
font-size: 24px;
color: var(--text-color);
}
</style>
</head>
<body data-theme="light">
<button id="btn">主题</button>
<div class="box">
<div class="title">我是帅哥!</div>
</div>
<script>
var btn = document.getElementById('btn')
btn.onclick = () => {
let atr = document.body.getAttribute('data-theme');
atr === 'dark' ? document.body.setAttribute('data-theme', 'light') : document.body.setAttribute('data-theme','dark')
}
</script>
</body>
</html>
css3新特性 var的换肤小demo
最新推荐文章于 2023-11-09 11:24:12 发布