如何使用 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