前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。
来看一个有意思的一键换肤,黑背景是白按钮,白背景是黑按钮:
它的源码如下:
<div id="textLight" class="text">Light</div>
<label class="switch">
<input id="toggle" type="checkbox">
<span class="slider round"></span>
</label>
<div id="textDark" class="textDark">Dark</div>
* {
margin: 0;
font-family: monospace;
font-size: 20px;
}
body {
height: 100vh;
justify-content: center;
align-items: center;
display: flex;
background-color: #0d1117;
color: #fff;
}
.light {
background-color: #fff !important;
color: #000;
}
.dark {
background-color: #0d1117 !important;
color: #fff;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: 0.4s;
transition: 0.4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: 0.4s;
transition: 0.4s;
}
input:checked + .slider {
background-color: #0d1117;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196f3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.text, .textDark {
margin: 10px;
border-radius: 10px;
padding: 10px;
}
const toggle = document.getElementById("toggle");
const body = document.querySelector("body");
const text = document.getElementById("textLight");
const textDark = document.getElementById("textDark");
toggle.addEventListener("click", function () {
if (toggle.checked) {
body.classList.add("light");
body.classList.remove("dark");
textDark.classList.add("light");
textDark.classList.remove("dark");
} else {
body.classList.add("dark");
body.classList.remove("light");
text.classList.add("dark");
text.classList.remove("light");
text.classList.remove("light");
}
});