Long and hard, I reinvented the bicycle in the form of a color switcher for the site, and I even thought that it worked for me until I reloaded the page. After a reboot, it is expected (but not for me) css flies to default. Google prompted to dig in the direction of localStorage , but nothing happened to me.
var colorSheets = [ { color: "#f2fcfb", title: "Светлая тема", href: "default.css" }, { color: "#000", title: "Тёмная тема", href: "dark.css" } ]; var ColorSwitcher = (function() { function initColorSwitcher(colorSheets) { var tempCon, colorSwitcher, controlBtn, colorSwitchs, linkHolderHtml, linkHolder; if (Object.prototype.toString.call(colorSheets) !== "[object Array]") { return; } tempCon = document.createDocumentFragment(); colorSwitcher = document.createElement("div"); colorSwitcher.classList.add("ColorSwitcher"); controlBtn = document.createElement("button"); controlBtn.classList.add("ColorSwitcher__control"); colorSwitchs = document.createElement("div"); colorSwitchs.classList.add("ColorSwitcher__switchs"); linkHolderHtml = document.createElement("link"); linkHolderHtml.rel = "stylesheet"; linkHolderHtml.id = "ColorSwitcherLinkHolder"; document.head.appendChild(linkHolderHtml); linkHolder = document.getElementById("ColorSwitcherLinkHolder"); colorSheets.forEach(function(colorSheet, index) { var colorSwitch; if (colorSheet.color && colorSheet.title && colorSheet.href) { colorSwitch = document.createElement("button"); colorSwitch.classList.add("ColorSwitcher__switch") colorSwitch.title = colorSheet.title; colorSwitch.dataset.index = index; colorSwitch.style.backgroundColor = colorSheet.color; colorSwitchs.appendChild(colorSwitch); } }); colorSwitchs.addEventListener("click", function(event) { var index; if (event.target.nodeName !== "BUTTON") { return; } index = event.target.dataset.index; linkHolder.href = colorSheets[index].href; return false; }); controlBtn.addEventListener("click", function(event) { event.target.parentElement.classList.toggle("ColorSwitcher--open"); return false; }); colorSwitcher.appendChild(controlBtn); colorSwitcher.appendChild(colorSwitchs); tempCon.appendChild(colorSwitcher); document.body.appendChild(tempCon); } return { init: initColorSwitcher }; })(); Tried to keep it:
colorSwitchs.addEventListener("click", function(event) { var index; if (event.target.nodeName !== "BUTTON") { return; } index = event.target.dataset.index; localStorage.setItem("key", index); linkHolder.href = colorSheets[index].href; return false; }); and this
colorSwitchs.addEventListener("click", function(event) { var index; if (event.target.nodeName !== "BUTTON") { return; } index = event.target.dataset.index; linkHolder.href = colorSheets[index].href; localStorage.setItem("key", linkHolder); return false; }); inside the function and outside of it, but localStorage was empty and stayed. I already gave up. Can I solve the problem with a couple of lines of the correct code, or do I need to start all over again?
Ps I am green
"use strict"; var colorSheets = [ { color: "#b0eae6", title: "Light", href: "default.css" }, { color: "#f3e686", title: "Gold", href: "gold.css" }, { color: "#000", title: "Dark", href: "dark.css" } ]; var ColorSwitcher = (function() { function initColorSwitcher(colorSheets) { var tempCon, colorSwitcher, controlBtn, colorSwitchs, linkHolderHtml, linkHolder; if (Object.prototype.toString.call(colorSheets) !== "[object Array]") { return; } tempCon = document.createDocumentFragment(); colorSwitcher = document.createElement("div"); colorSwitcher.classList.add("ColorSwitcher"); controlBtn = document.createElement("button"); controlBtn.classList.add("ColorSwitcher__control"); colorSwitchs = document.createElement("div"); colorSwitchs.classList.add("ColorSwitcher__switchs"); linkHolderHtml = document.createElement("link"); linkHolderHtml.rel = "stylesheet"; linkHolderHtml.id = "ColorSwitcherLinkHolder"; document.head.appendChild(linkHolderHtml); linkHolder = document.getElementById("ColorSwitcherLinkHolder"); colorSheets.forEach(function(colorSheet, index) { var colorSwitch; if (colorSheet.color && colorSheet.title && colorSheet.href) { colorSwitch = document.createElement("button"); colorSwitch.classList.add("ColorSwitcher__switch") colorSwitch.title = colorSheet.title; colorSwitch.dataset.index = index; colorSwitch.style.backgroundColor = colorSheet.color; colorSwitchs.appendChild(colorSwitch); } }); colorSwitchs.addEventListener("click", function(event) { var index; if (event.target.nodeName !== "BUTTON") { return; } index = event.target.dataset.index; linkHolder.href = colorSheets[index].href; return false; }); controlBtn.addEventListener("click", function(event) { event.target.parentElement.classList.toggle("ColorSwitcher--open"); return false; }); colorSwitcher.appendChild(controlBtn); colorSwitcher.appendChild(colorSwitchs); tempCon.appendChild(colorSwitcher); document.body.appendChild(tempCon); } return { init: initColorSwitcher }; })(); .ColorSwitcher, .ColorSwitcher * { box-sizing: border-box; } .ColorSwitcher { position: fixed; top: 50%; left: -162px; width: 162px; padding: 20px 10px; background: #fff; border-radius: 0 3px 3px 0; box-shadow: 0 0 15px rgba(0,0,0,.15); -webkit-transform: translateY(-80%); transform: translateY(-80%); -webkit-transition: left .2s; transition: left .2s; } .ColorSwitcher--open { left: 0; } .ColorSwitcher__control, .ColorSwitcher__switch { display: inline-block; width: 40px; height: 40px; padding: 0; border: 0; cursor: pointer; -webkit-transition: all .2s; transition: all .2s; } .ColorSwitcher__control:focus, .ColorSwitcher__switch:focus { outline: 0; } .ColorSwitcher__control { position: absolute; right: 0; left: 100%; border-radius: 0 3px 3px 0; box-shadow: 5px 0 7px rgba(0,0,0,.15); color: #fff; background: #ff463a; } .ColorSwitcher__control:before { content: ""; display: inline-block; height: 100%; width: 100%; background-size: 70%; background-position: center; background-repeat: no-repeat; } .ColorSwitcher--open .ColorSwitcher__control { background: #fff; } .ColorSwitcher__switchs { margin: -5px; } .ColorSwitcher__switch { border-radius: 3px; margin: 5px; } @-webkit-keyframes controlSpin { 100% { -webkit-transform:rotate(360deg); transform:rotate(360deg); } } @keyframes controlSpin { 100% { -webkit-transform:rotate(360deg); transform:rotate(360deg); } } <body> </body> <script> ColorSwitcher.init(colorSheets); </script>
initColorSwitchercall in the attached code. - IgorinitColorSwitchercalled inhtml- SjeurtlocalStorage.getItem? - Nikita UmnovlocalStorage.setItems("key","value")The corresponding keys appear in the local storage. - Sjeurt