溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

JavaScript或jQuery如何實(shí)現(xiàn)網(wǎng)站夜間/高亮模式

發(fā)布時(shí)間:2020-07-20 15:52:32 來(lái)源:億速云 閱讀:172 作者:小豬 欄目:web開(kāi)發(fā)

小編這次要給大家分享的是JavaScript或jQuery如何實(shí)現(xiàn)網(wǎng)站夜間/高亮模式,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

創(chuàng)建夜間/高亮模式的步驟:

創(chuàng)建一個(gè)HTML文檔。

為文檔文件以及黑暗模式創(chuàng)建CSS。

添加一個(gè)開(kāi)關(guān)轉(zhuǎn)換器按鈕,以在明暗模式之間進(jìn)行切換。

使用javascript或jQuery代碼向開(kāi)關(guān)轉(zhuǎn)換器添加功能,以在明暗模式之間切換。

示例1:以下示例演示了使用JQuery代碼在明暗模式之間進(jìn)行切換。它基本上通過(guò)使用函數(shù)hasClass(),addClass()和removeClass()方法來(lái)工作。

<!DOCTYPE html>
<html lang="en">
  
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
      Dark Mode
    </title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js">
    </script>
    <style>
      body{ padding:10% 3% 10% 3%; text-align:center; } img{ height:140px; width:140px;
      } h2{ color: #32a852; } .mode { float:right; } .change { cursor: pointer;
      border: 1px solid #555; border-radius: 40%; width: 20px; text-align: center;
      padding: 5px; margin-left: 8px; } .dark{ color: #e6e6e6; }
    </style>
  </head>
  
  <body>
    <div class="mode">
      Dark mode:
      <span class="change">
        OFF
      </span>
    </div>
    <div>
      <h2>
        GeeksforGeeks
      </h2>
      <p>
        <i>
          A Computer Science Portal for Geeks
        </i>
      </p>
      <h4>
        Light and Dark Mode
      </h4>
      <img src="https://cache.yisu.com/upload/information/20200622/114/1684.png">
      <p>
        Click on the switch on top-right to move to dark mode.
      </p>
    </div>
    <script>
      $(".change").on("click",
      function() {
        if ($("body").hasClass("dark")) {
          $("body").removeClass("dark");
          $(".change").text("OFF");
        } else {
          $("body").addClass("dark");
          $(".change").text("ON");
        }
      });
    </script>
  </body>

</html>

示例2:以下示例演示了通過(guò)在JavaScript代碼中使用toggle()函數(shù)在高亮模式和夜間模式之間進(jìn)行切換。

<!DOCTYPE html>
<html lang="en">
  
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
      Dark Mode
    </title>
    <style>
      body{ padding:0% 3% 10% 3%; text-align:center; } h2{ color: #32a852; margin-top:30px;
      } button{ cursor: pointer; border: 1px solid #555; text-align: center;
      padding: 5px; margin-left: 8px; } .dark{ color: #e6e6e6; }
    </style>
  </head>
  
  <body>
    <h2>
      GeeksforGeeks
    </h2>
    <p>
      <i>
        A Computer Science Portal for Geeks
      </i>
    </p>
    <h4>
      Light and Dark Mode
    </h4>
    <button onclick="myFunction()">
      Switch mode
    </button>
    <script>
      function myFunction() {
        var element = document.body;
        element.classList.toggle("dark");
      }
    </script>
  </body>

</html>

看完這篇關(guān)于JavaScript或jQuery如何實(shí)現(xiàn)網(wǎng)站夜間/高亮模式的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI