• 如何使用 JavaScript/jQuery 为网站创建暗/亮模式?


    深色模式对于任何网站都非常重要。不同兴趣的用户访问网站。有些人喜欢深色模式,有些人喜欢浅色模式。

    根据一项调查,大约 70% 到 80% 的人喜欢深色模式,只有 20% 到 30% 的人喜欢浅色模式。因此,有必要为任何网站创建一个深色模式,允许用户在深色和浅色模式之间切换。

    下面,我们将使用 HTML、CSS 和 JavaScript 创建一个简单的网页。此外,我们还将学习使用 JavaScript 和 CSS 实现明暗模式。

    语法

    用户可以按照以下语法在深色和浅色主题之间切换。

    body.classList.toggle("dark-theme");

     

    在上面的语法中,我们将深色主题类名作为切换方法的参数传递。它在特定的 HTML 元素中添加和删除类。

    算法

    用户可以按照以下算法在深色和浅色主题之间切换。

    • 第 1 步 - 为网页编写 HTML 代码,并像往常一样为浅色主题应用 CSS。

    • 第 2 步 - 使用 CSS 创建深色主题的样式。

    • 第 3 步 - 创建一个按钮,并在按钮中添加 onclick 事件。当用户单击该按钮时,它应该在深色和浅色主题之间切换。

    • 步骤 4 - 要在深色和浅色主题之间切换,我们可以从元素中添加和删除特定的类。例如,为深色主题创建一个类,并为深色主题创建 CSS。当我们在body中添加dark-theme类时,深色主题应该应用于整个网站,当我们删除它时,它应该是正常的。

    示例 1

    在下面的示例中,当用户单击按钮时,我们调用changeMode()函数。 changeMode() 函数根据主题更改按钮的文本。

    此外,我们使用 document.body 访问整个网页,并使用 JavaScript 将 dark-theme 类添加到整个网页,以将主题更改为深色模式。

    1. body {
    2. color: black;
    3. background-color: rgb(241, 241, 241);
    4. text-align: center;
    5. justify-content: center;
    6. }
    7. .dark-theme {
    8. color: white;
    9. background-color: rgb(26, 20, 20);
    10. }
    11. button {
    12. width: 13rem;
    13. height: 2rem;
    14. font-size: 1.5rem;
    15. background-color: aqua;
    16. border: none;
    17. border-radius: 12px;
    18. }
    19. p {
    20. font-size: 1.2rem;
    21. }
    22. Using the toggle method to toggle between light and dark mode in JavaScript.
    23. This is the content of the webpage.
    24. Dark Mode
    25. function changeMode() {
    26. var body = document.body;
    27. // toggle the theme
    28. body.classList.toggle("dark-theme");
    29. let button = document.getElementById('button');
    30. // change the button text
    31. if (button.innerHTML == "Dark Mode") {
    32. button.innerHTML = "Normal Mode";
    33. } else {
    34. button.innerHTML = "Dark Mode"
    35. }
    36. }

     

    示例 2

    在下面的示例中,我们使用 HTML 和 CSS 创建了切换开关按钮。我们使用了切换开关的复选框。因此,每当用户选中该复选框时,开关就会打开。因此,我们可以根据是否选中该复选框来添加和删除深色主题的类。

    此外,我们还使用了JQuery的addClass()和removeClass()方法来添加和删除主体中的类。

    1. body {
    2. color: black;
    3. background-color: rgb(241, 241, 241);
    4. text-align: center;
    5. justify-content: center;
    6. }
    7. .dark-class {
    8. color: white;
    9. background-color: rgb(12, 10, 10);
    10. }
    11. p {
    12. font-size: 1.2rem;
    13. }
    14. .toggleButton {
    15. width: 5rem;
    16. height: 2rem;
    17. position: relative;
    18. display: inline-block;
    19. }
    20. .toggleButton input {
    21. opacity: 0;
    22. }
    23. .roundButton {
    24. background-color: black;
    25. top: 0;
    26. left: 0;
    27. position: absolute;
    28. right: 0;
    29. bottom: 0;
    30. cursor: pointer;
    31. }
    32. .roundButton:before {
    33. left: 0;
    34. bottom: 0;
    35. position: absolute;
    36. content: "";
    37. background-color: grey;
    38. transition: 1s;
    39. height: 2rem;
    40. width: 2rem;
    41. }
    42. input:checked+.roundButton {
    43. background-color: white;
    44. }
    45. input:checked+.roundButton:before {
    46. transform: translateX(3rem);
    47. }
    48. .roundButton.circle {
    49. border-radius: 2rem;
    50. }
    51. .roundButton.circle:before {
    52. border-radius: 50%;
    53. }
    54. Using the JQuery to toggle between light and dark modes in JavaScript.
    55. This is the content of the webpage.
    56. // If the input checkbox is checked, activate dark mode by adding dark-class to the body
    57. $('#toggle').change(() => {
    58. if ($('#toggle').is(":checked")) {
    59. $("body").addClass("dark-class");
    60. } else {
    61. $("body").removeClass("dark-class")
    62. }
    63. })

     我们学会了使用 JavaScript 和 JQuery 在深色模式和浅色模式之间进行切换。我们需要更改网页的 CSS 并将 CSS 应用于深色主题。我们可以通过在网页中添加和删除类来将 CSS 应用到暗模式。

  • 相关阅读:
    Java笔记【方法、数组】
    web:[极客大挑战 2019]Knife
    分布式爬虫与SOCKS5代理池的组合优势
    小孩近视用白炽灯好吗?使用护眼台灯有啥好处?
    希望所有计算机学生都知道这些宝藏网站
    Python 图形化界面基础篇:使用网格布局( Grid Layout )排列元素
    设计帅气的游戏鼠标,手感真不一般,雷柏VT960S上手
    Spirngboot中文乱码解决方案
    2022年6月电子学会Python等级考试试卷(一级)答案解析
    性能调优MySQL 一
  • 原文地址:https://blog.csdn.net/lwf3115841/article/details/132893029