英文 | https://www.geeksforgeeks.org/how-to-create-dark-light-mode-for-website-using-javascript-jquery/?ref=leftbar-rightbar
翻译 | web前端开发(ID:web_qdkf)
浅色深色配色方案(也称为深色模式)是一种补充模式,它使用一种配色方案,其中网页的内容显示在深色背景上。这种配色方案减少了屏幕发出的光并增强了可读性。切换为夜间模式可让网站用户随时随地转向外观友好且节省资源的设计。
创建夜间/高亮模式的步骤:
创建一个HTML文档。
为文档文件以及黑暗模式创建CSS。
添加一个开关转换器按钮,以在明暗模式之间进行切换。
使用javascript或jQuery代码向开关转换器添加功能,以在明暗模式之间切换。
示例1:以下示例演示了使用JQuery代码在明暗模式之间进行切换。它基本上通过使用函数hasClass(),addClass()和removeClass()方法来工作。
content="width=device-width, initial-scale=1.0">
Dark Mode
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js">
body{
padding:10% 3% 10% 3%;
text-align:center;
}
img{
height:140px;
width:140px;
}
h1{
color: #32a852;
}
.mode {
float:right;
}
.change {
cursor: pointer;
border: 1px solid #555;
border-radius: 40%;
width: 20px;
text-align: center;
padding: 5px;
margin-left: 8px;
}
.dark{
background-color: #222;
color: #e6e6e6;
}
Dark mode:
OFF
GeeksforGeeks
GeeksforGeeks
A Computer Science Portal for Geeks
Light and Dark Mode
Light and Dark Mode
![span]()
"https://media.geeksforgeeks.org/wp-content/uploads/20200122115631/GeeksforGeeks210.png">
Click on the switch on top-right
to move to dark mode.
输出:
高亮模式:
![d60c3fdd9fcb47d63014fe0c073e7bf6.png](https://img-blog.csdnimg.cn/img_convert/d60c3fdd9fcb47d63014fe0c073e7bf6.png)
![2c753c9a54ee237ef4ef92658d92c215.png](https://img-blog.csdnimg.cn/img_convert/2c753c9a54ee237ef4ef92658d92c215.png)
"width=device-width, initial-scale=1.0">
Dark Mode
body{
padding:0% 3% 10% 3%;
text-align:center;
}
h1{
color: #32a852;
margin-top:30px;
}
button{
cursor: pointer;
border: 1px solid #555;
text-align: center;
padding: 5px;
margin-left: 8px;
}
.dark{
background-color: #222;
color: #e6e6e6;
}
GeeksforGeeks
GeeksforGeeks
A Computer Science Portal for Geeks
Light and Dark Mode
Light and Dark Mode
Switch mode
输出:
高亮模式:
![32e751169d05d502c359977af4ec29c4.png](https://img-blog.csdnimg.cn/img_convert/32e751169d05d502c359977af4ec29c4.png)
![312cc01853cdca5dca182a8179cf1dfc.png](https://img-blog.csdnimg.cn/img_convert/312cc01853cdca5dca182a8179cf1dfc.png)
![562f3a07af408f45a9b79750979a6cfd.png](https://img-blog.csdnimg.cn/img_convert/562f3a07af408f45a9b79750979a6cfd.png)
![0662422eb1b44b3e74b26b360f709bdb.png](https://img-blog.csdnimg.cn/img_convert/0662422eb1b44b3e74b26b360f709bdb.png)