0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ダークモードの実装方法

Posted at

必要な部分のみ書いています。

index.html
<head>
  <link rel="stylesheet" type="text/css" id="dark-mode-link" href="./css/dark-mode.css">
</head>

<body>
  <p>ダークモード!</p>

  <script>
    const darkModetoggleSwitch = document.querySelector("#darkModetoggleSwitch")
    const darkModeLink = document.querySelector('#dark-mode-link');

    darkModetoggleSwitch.addEventListener('click', toggleDarkMode);

    function toggleDarkMode() {
      if (darkModeLink.href.includes('dark-mode.css')) {
        darkModeLink.href = '';
      } else {
        darkModeLink.href = 'dark-mode.css';
      }
    }
  </script>
</body>
dark-mode.css
body {
  background-color: #121212;
  color: #ffffff;
}

手動で切り替える方法。ボタンでの切り替え実装。参考サイトではローカルに保存して、最初の読み込み時に分けていたりしてます。

@media (prefers-color-scheme: dark) {
  body {
    background-color: #121212;
    color: #ffffff;
  }
 }

OSの設定で切り替える方法です。mac、windows、ios、androidどれでもいけると思います。

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?