javascript实现点击主题按钮切换主题
·
通过点击主题按钮修改颜色
实现效果:
点击元素实现切换主题
html代码:
<!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>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<ul>
<li class="theme-1">主题1</li>
<li class="theme-2">主题2</li>
<li class="theme-3">主题3</li>
</ul>
<div class="article theme-1" id="article">
<div class="header">这是一首诗</div>
<div class="main">
<div class="art-title">锄禾</div>
<div class="art-content">
锄禾日当午,<br>
汗滴禾下土。<br>
谁知盘中餐,<br>
粒粒皆辛苦。<br>
</div>
</div>
<div class="footer">作者:李绅</div>
</div>
<script src="./index.js"></script>
</body>
</html>
css代码:
* {
margin: 0;
padding: 0;
list-style: none;
}
ul {
display: flow-root;
margin-left: 100px;
cursor: pointer;
margin-top: 30px;
}
li {
width: 80px;
height: 40px;
line-height: 40px;
text-align: center;
margin-right: 50px;
color: #fff;
float: left;
}
li.theme-1 {
background-color: red;
}
li.theme-2 {
background-color: blue;
}
li.theme-3 {
background-color: green;
}
.article {
width: 240px;
margin: 30px 0 0 150px;
text-align: center;
}
.header {
height: 60px;
line-height: 60px;
font-size: 24px;
color: #fff;
font-weight: 500;
padding-left: 10px;
}
.main {
padding: 20px;
width: 200px;
}
.art-title {
font-size: 22px;
font-weight: 700;
height: 60px;
line-height: 60px;
}
.footer {
height: 40px;
line-height: 40px;
color: black;
}
/* 抽取公共样式 */
/* .theme-1 */
.theme-1 .header {
background-color: red;
}
.theme-1 .main {
background-color: #ecf5ff;;
}
.theme-1 .art-content {
color: red;
}
.theme-1 .footer {
background-color: red;
}
/* .theme-2 */
.theme-2 .header {
background-color: blue;
}
.theme-2 .main {
background-color: #e1f3d8;;
}
.theme-2 .art-content {
color: blue;
}
.theme-2 .footer {
background-color: blue;
}
/* .theme-3 */
.theme-3 .header {
background-color: green;
}
.theme-3 .main {
background-color: #fde2e2;;
}
.theme-3 .art-content {
color: green;
}
.theme-3 .footer {
background-color: green;
}
JS代码:
var lis = document.getElementsByTagName('li');
var article = document.getElementById('article');
lis[0].onclick = function() {
article.className = 'article theme-1';
}
lis[1].onclick = function() {
article.className = 'article theme-2';
}
lis[2].onclick = function() {
article.className = 'article theme-3';
}
更多推荐



所有评论(0)