如何使用 JavaScript/jQuery 為網站建立深色/淺色模式?
深色模式對於任何網站都非常重要。不同興趣的使用者訪問網站。有些人喜歡深色模式,有些人喜歡淺色模式。
根據一項調查,大約 70% 到 80% 的人喜歡深色模式,只有 20% 到 30% 的人喜歡淺色模式。因此,為任何網站建立深色模式都是必要的,允許使用者在深色和淺色模式之間切換。
下面,我們將使用 HTML、CSS 和 JavaScript 建立一個簡單的網頁。此外,我們將學習如何使用 JavaScript 和 CSS 實現淺色和深色模式。
語法
使用者可以按照以下語法在深色和淺色主題之間切換。
body.classList.toggle("dark-theme");
在上面的語法中,我們將深色主題類名作為 toggle 方法的引數傳遞。它從特定 HTML 元素中新增和刪除類。
演算法
使用者可以按照以下演算法在深色和淺色主題之間切換。
步驟 1 - 為網頁編寫 HTML 程式碼,並像往常一樣應用 CSS 以實現淺色主題。
步驟 2 - 使用 CSS 建立深色主題的樣式。
步驟 3 - 建立一個按鈕,並在按鈕中新增 onclick 事件。當用戶點選按鈕時,它應該在深色和淺色主題之間切換。
步驟 4 - 為了在深色和淺色主題之間切換,我們可以從元素中新增和刪除特定的類。例如,為深色主題建立一個類和深色主題的 CSS。當我們將深色主題類新增到 body 時,深色主題應該應用於整個網站,而當我們將其刪除時,它應該恢復正常。
示例 1
在下面的示例中,當用戶點選按鈕時,我們將呼叫 changeMode() 函式。changeMode() 函式根據主題更改按鈕的文字。
此外,我們使用 document.body 訪問了整個網頁,並使用 JavaScript 將 dark-theme 類新增到整個 body 以將主題更改為深色模式。
<html>
<head>
<style>
body {
color: black;
background-color: rgb(241, 241, 241);
text-align: center;
justify-content: center;
}
.dark-theme {
color: white;
background-color: rgb(26, 20, 20);
}
button {
width: 13rem;
height: 2rem;
font-size: 1.5rem;
background-color: aqua;
border: none;
border-radius: 12px;
}
p {
font-size: 1.2rem;
}
</style>
</head>
<body>
<h2>Using the <i> toggle method to </i> toggle between light and dark mode in JavaScript. </h2>
<p>This is the content of the webpage. </p>
<button onclick = "changeMode()" id = "button"> Dark Mode </button>
<script>
function changeMode() {
var body = document.body;
// toggle the theme
body.classList.toggle("dark-theme");
let button = document.getElementById('button');
// change the button text
if (button.innerHTML == "Dark Mode") {
button.innerHTML = "Normal Mode";
} else {
button.innerHTML = "Dark Mode"
}
}
</script>
</body>
</html>
示例 2
在下面的示例中,我們使用 HTML 和 CSS 建立了切換按鈕。我們使用了複選框作為切換開關。因此,每當使用者選中複選框時,開關就會開啟。因此,我們可以根據複選框是否選中來新增和刪除深色主題的類。
此外,我們使用了 JQuery 的 addClass() 和 removeClass() 方法來從 body 中新增和刪除類。
<html>
<head>
<style>
body {
color: black;
background-color: rgb(241, 241, 241);
text-align: center;
justify-content: center;
}
.dark-class {
color: white;
background-color: rgb(12, 10, 10);
}
p {
font-size: 1.2rem;
}
.toggleButton {
width: 5rem;
height: 2rem;
position: relative;
display: inline-block;
}
.toggleButton input {
opacity: 0;
}
.roundButton {
background-color: black;
top: 0;
left: 0;
position: absolute;
right: 0;
bottom: 0;
cursor: pointer;
}
.roundButton:before {
left: 0;
bottom: 0;
position: absolute;
content: "";
background-color: grey;
transition: 1s;
height: 2rem;
width: 2rem;
}
input:checked+.roundButton {
background-color: white;
}
input:checked+.roundButton:before {
transform: translateX(3rem);
}
.roundButton.circle {
border-radius: 2rem;
}
.roundButton.circle:before {
border-radius: 50%;
}
</style>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"
Integrity = "sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ=="
crossorigin = "anonymous" referrerpolicy = "no-referrer"> </script>
</head>
<body>
<h2>Using the <i> JQuery </i> to toggle between light and dark modes in JavaScript.</h2>
<p>This is the content of the webpage.</p>
<label class = "toggleButton">
<input type = "checkbox" id = "toggle">
<div class = "roundButton circle" ></div>
</label>
<script>
// If the input checkbox is checked, activate dark mode by adding dark-class to the body
$('#toggle').change(() => {
if ($('#toggle').is(":checked")) {
$("body").addClass("dark-class");
} else {
$("body").removeClass("dark-class")
}
})
</script>
</body>
</html>
我們學習瞭如何使用 JavaScript 和 JQuery 在深色和淺色模式之間切換。我們需要更改網頁的 CSS 並應用深色主題的 CSS。我們可以透過新增和刪除網頁的類來應用深色模式的 CSS。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP