新增第200个小实例:网站实现一键切换暗色主题
This commit is contained in:
parent
49e7106255
commit
c92ae104a5
|
@ -207,6 +207,7 @@
|
|||
197. HTML5+CSS3+Vue小实例:彩带圣诞树
|
||||
198. HTML5+CSS3+JS小实例:网页手电筒
|
||||
199. HTML5+CSS3小实例:不一样的超链接下划线
|
||||
200. HTML5+CSS3+JS小实例:网站实现一键切换暗色主题
|
||||
|
||||
#### 赞赏作者
|
||||
![image](https://gitee.com/wyanhui02/html_css_demo/raw/master/images/%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85/%E8%B5%9E%E8%B5%8F%E7%A0%81.jpg)
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
*{
|
||||
/* 初始化 */
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
body{
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
/* logo */
|
||||
img{
|
||||
width: 270px;
|
||||
}
|
||||
/* 搜索框 */
|
||||
.ipt-box{
|
||||
width: 650px;
|
||||
display: flex;
|
||||
margin-top: 20px;
|
||||
margin-bottom: 200px;
|
||||
}
|
||||
.ipt-box input{
|
||||
flex: 1;
|
||||
border: 2px solid #c4c7ce;
|
||||
border-radius: 10px 0 0 10px;
|
||||
border-right: none;
|
||||
}
|
||||
.ipt-box button{
|
||||
width: 108px;
|
||||
height: 44px;
|
||||
background-color: #4e6ef2;
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 0 10px 10px 0;
|
||||
font-size: 17px;
|
||||
}
|
||||
/* 切换按钮 */
|
||||
.switch{
|
||||
border: 1px solid #c4c7ce;
|
||||
width: 60px;
|
||||
height: 30px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 0 6px;
|
||||
border-radius: 15px;
|
||||
cursor: pointer;
|
||||
position: fixed;
|
||||
top: 15px;
|
||||
right: 15px;
|
||||
}
|
||||
/* 滑块 */
|
||||
.switch::before{
|
||||
content: '';
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
border-radius: 50%;
|
||||
background-color: #4e6ef2;
|
||||
position: absolute;
|
||||
left: 3px;
|
||||
transition: 0.2s ease-out;
|
||||
}
|
||||
.switch.dark::before{
|
||||
left: 31px;
|
||||
}
|
||||
/* 图标 */
|
||||
.switch .fa{
|
||||
font-size: 18px;
|
||||
color: #aaa;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<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="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
|
||||
<link rel="stylesheet" href="200.css">
|
||||
</head>
|
||||
<body>
|
||||
<img src="/images/bd_logo.png" alt="">
|
||||
<div class="ipt-box">
|
||||
<input type="text">
|
||||
<button>百度一下</button>
|
||||
</div>
|
||||
<div class="switch">
|
||||
<i class="fa fa-sun-o"></i>
|
||||
<i class="fa fa-moon-o"></i>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<script src="200.js"></script>
|
|
@ -0,0 +1,18 @@
|
|||
// 切换按钮
|
||||
const btn=document.querySelector('.switch');
|
||||
|
||||
btn.addEventListener('click',function(){
|
||||
let is_dark=document.querySelector('.switch.dark');
|
||||
if(!is_dark){
|
||||
// 切换为暗色主题
|
||||
btn.classList.add('dark');
|
||||
let a=document.createElement('style');
|
||||
a.id='aa';
|
||||
a.innerHTML='html{background-color:#fff;filter:invert(1);}img{filter:invert(1);}';
|
||||
document.head.appendChild(a);
|
||||
}else{
|
||||
// 切换为亮色主题
|
||||
btn.classList.remove('dark');
|
||||
document.head.querySelector('#aa').remove();
|
||||
}
|
||||
})
|
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
Loading…
Reference in New Issue