繼 Dark Mode(壹),又寫了一個復雜點的,效果如圖所示,

目標更新十種dark mode的寫法!在線預覽鏈接,源代碼也在~
talk is cheap,show me the code!
代碼很簡單,沒有晦澀的部分,就不做解釋了🤪
HTML
用checkbox來實作切換主題
<h1>Mancuoj</h1>
<label class="dark-label" for="dark-checkbox">
<input id="dark-checkbox" type="checkbox">
<span class="ball"> </span>
<svg class="sun-svg" viewBox="0 0 512 512">
<path d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"></path>
</svg>
<svg class="moon-svg" viewBox="0 0 512 512">
<path d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 0 0 283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"></path>
</svg>
</label>
SCSS一些基礎設定
第二天重構了一下~
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap");
$dark-color: #111;
$light-color: #eee;
$bg-color: #4c6fff;
*,
* *,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
font-family: Poppins, serif;
}
h1 {
margin-right: 20px;
user-select: none;
}
調整位置+影片
.dark-label {
display: block;
margin: 20px 0;
height: 31px;
width: 60px;
border: 2px solid $bg-color;
border-radius: 30px;
position: relative;
cursor: pointer;
.ball {
position: absolute;
width: 25px;
height: 25px;
top: 1px;
left: 1px;
border-radius: 50%;
background-color: $bg-color;
transition: 300ms;
z-index: 99;
}
#dark-checkbox {
position: absolute;
visibility: hidden;
}
#dark-checkbox:checked + .ball {
transform: translateX(28px);
}
.moon-svg,
.sun-svg {
width: 16px;
height: 16px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.sun-svg {
left: 5px;
}
.moon-svg {
right: 5px;
}
}
dark mode
.dark {
background-color: $dark-color;
color: $light-color;
.sun-svg,
.moon-svg {
fill: $light-color;
}
}
JS
寫個點擊切換的函式,其中用到了localStorage特性:
在HTML5中新加入了一個localStorage特性,這個特性主要是用來作為本地存盤來使用的,解決了cookie存盤空間不足的問題(cookie中每條cookie的存盤空間為4k),localStorage中一般瀏覽器支持的是5M大小,這個在不同的瀏覽器中localStorage會有所不同,
localStorage會可以將第一次請求的資料直接存盤到本地,相當于一個5M大小的針對于前端頁面的資料庫,相比于cookie可以節約帶寬,

const changeTheme = () => {
const darkCheck = document.getElementById("dark-checkbox");
darkCheck.addEventListener("click", () => {
if (darkCheck.checked) {
document.body.classList.add("dark");
localStorage.setItem("css", "dark");
} else {
document.body.classList.remove("dark");
localStorage.removeItem("css");
}
});
if (localStorage.getItem("css")) {
document.body.className = "dark";
darkCheck.checked = true;
}
};
changeTheme();
好水,但是實在沒啥解釋的空間,見諒~
我是Macnuoj,歡迎關注互相交流
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/347187.html
標籤:其他
