Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

黑白主题切换

Pcjmy opened this issue · comments

commented
<button>关灯</button>
    <script>
        var btn = document.querySelector('button');
        var bd = document.querySelector('body');
        btn.onclick = function () {
            if (btn.innerHTML === '关灯') {
                bd.style.backgroundColor = '#111';
                btn.innerHTML = '开灯';
            }
            else {
                bd.style.backgroundColor = '#fff';
                btn.innerHTML = '关灯';
            }
        }
    </script>

纯css版本

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .content {
        width: 100%;
        height: 600px;
        transition: all 1s;
      }
      #modeCheckBox {
        display: none;
      }
      #modeCheckBox:checked + .content {
        background-color: black;
        color: white;
        transition: all 1s;
      }
      #modeBtn {
        font-size: 2rem;
        float: right;
      }
      #modeBtn::after {
        content: '🌞';
      }
      #modeCheckBox:checked + .content #modeBtn::after {
        content: '🌜';
      }
    </style>
  </head>
  <body>
    <input id="modeCheckBox" type="checkbox" />
    <div class="content">
      <header>
        <label id="modeBtn" for="modeCheckBox"></label>
      </header>
    </div>
  </body>
</html>