Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!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>
- </head>
- <body class="modByTheme">
- <header>My page</header>
- <button class="themeToggler"></button>
- <p class="modByTheme">This is a paragraph</p>
- <b class="modByTheme">This is bold text</b>
- <br />
- <i class="modByTheme">italic text</i>
- <div class="modByTheme" id="last"></div>
- </body>
- <style>
- .light-theme {
- color: rgb(4, 33, 202);
- background-color: seashell;
- }
- .dark-theme {
- color: white;
- background-color: rgb(11, 20, 20);
- }
- header {
- padding: 3.5%;
- background-color: purple;
- text-align: center;
- text-decoration: underline;
- font-size: x-large;
- }
- #last {
- padding: 30%;
- }
- </style>
- <script>
- "use strict";
- window.onload = () => {
- console.log("asd");
- if (typeof Storage == undefined) {
- document.body.innerHTML = "Your browser is to weak!";
- return;
- }
- if (localStorage.getItem("theme") == null) {
- localStorage.setItem("theme", "light");
- }
- applyTheme();
- };
- const getOpTheme = (theme) => {
- return theme === "dark" ? "light" : "dark";
- };
- const applyTheme = () => {
- const theme = localStorage.getItem("theme");
- themeToggler.innerHTML = getOpTheme(theme);
- const modByThemeElems = document.querySelectorAll(".modByTheme");
- //console.log(theme);
- for (let elem of modByThemeElems) {
- if (elem.classList.length == 2) {
- elem.classList.remove(`${getOpTheme(theme)}-theme`);
- }
- elem.classList.add(`${theme}-theme`);
- } //*/
- };
- const themeToggler = document.querySelector(".themeToggler");
- themeToggler.addEventListener("click", (event) => {
- event.preventDefault();
- let newTheme = getOpTheme(localStorage.getItem("theme"));
- localStorage.setItem("theme", newTheme);
- applyTheme();
- });
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement