Advertisement
STANAANDREY

theme togller

Mar 22nd, 2021
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7.     <title>Document</title>
  8.   </head>
  9.   <body class="modByTheme">
  10.     <header>My page</header>
  11.     <button class="themeToggler"></button>
  12.     <p class="modByTheme">This is a paragraph</p>
  13.     <b class="modByTheme">This is bold text</b>
  14.     <br />
  15.     <i class="modByTheme">italic text</i>
  16.     <div class="modByTheme" id="last"></div>
  17.   </body>
  18.   <style>
  19.     .light-theme {
  20.       color: rgb(4, 33, 202);
  21.       background-color: seashell;
  22.     }
  23.  
  24.     .dark-theme {
  25.       color: white;
  26.       background-color: rgb(11, 20, 20);
  27.     }
  28.  
  29.     header {
  30.       padding: 3.5%;
  31.       background-color: purple;
  32.       text-align: center;
  33.       text-decoration: underline;
  34.       font-size: x-large;
  35.     }
  36.  
  37.     #last {
  38.       padding: 30%;
  39.     }
  40.   </style>
  41.   <script>
  42.     "use strict";
  43.     window.onload = () => {
  44.       console.log("asd");
  45.       if (typeof Storage == undefined) {
  46.         document.body.innerHTML = "Your browser is to weak!";
  47.         return;
  48.       }
  49.       if (localStorage.getItem("theme") == null) {
  50.         localStorage.setItem("theme", "light");
  51.       }
  52.       applyTheme();
  53.     };
  54.  
  55.     const getOpTheme = (theme) => {
  56.       return theme === "dark" ? "light" : "dark";
  57.     };
  58.  
  59.     const applyTheme = () => {
  60.       const theme = localStorage.getItem("theme");
  61.       themeToggler.innerHTML = getOpTheme(theme);
  62.       const modByThemeElems = document.querySelectorAll(".modByTheme");
  63.       //console.log(theme);
  64.       for (let elem of modByThemeElems) {
  65.         if (elem.classList.length == 2) {
  66.           elem.classList.remove(`${getOpTheme(theme)}-theme`);
  67.         }
  68.         elem.classList.add(`${theme}-theme`);
  69.       } //*/
  70.     };
  71.  
  72.     const themeToggler = document.querySelector(".themeToggler");
  73.     themeToggler.addEventListener("click", (event) => {
  74.       event.preventDefault();
  75.       let newTheme = getOpTheme(localStorage.getItem("theme"));
  76.       localStorage.setItem("theme", newTheme);
  77.       applyTheme();
  78.     });
  79.   </script>
  80. </html>
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement