本文实例为大家分享了js实现一键换肤效果的具体代码,供大家参考,具体内容如下
方法1

<!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>一键换肤</title>
<style>
:root {
--color: white;
}
.card {
width: 120px;
height: 200px;
}
.controller {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
}
.btn {
border: none;
height: 30px;
width: 100px;
color: #eeeeee;
background: linear-gradient(45deg, #ce7777, lightblue, #c19fc1, transparent);
border-radius: 999px;
box-shadow: 0 0 2px 2px #eeeeee;
cursor: pointer;
}
.card {
border-radius: 5px;
box-shadow: 0 0 2px 2px rgb(126, 124, 124);
}
.card:nth-child(1) {
background: black;
}
.card:nth-child(3) {
background: white;
}
html, body {
background: var(--color);
opacity: 0.9;
}
body {
height: 100vh;
}
*{
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div class="controller">
<div class="card"></div>
<div><button class="btn" onclick="changeSkin()">一键换肤</button></div>
<div class="card"></div>
</div>
</body>
</html>
方法2
<!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>change skin</title>
<style id="theme">
:root {
--bgColor: #f00;
}
.skin {
background: var(--bgColor);
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="skin"></div>
<button type="button" onclick="changeSkin('black')">change theme</button>
<script>
changeSkin = (theme) => {
console.log("function starts");
document.getElementById("theme").innerHTML = `
:root{--bgColor:${theme};}
.skin {
background: var(--bgColor);
width: 200px;
height: 200px;
}
`
}
</script>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)