From c2fd8793a0f7c0b092d33e4b759f5974efc69392 Mon Sep 17 00:00:00 2001 From: Ilia Miheev Date: Sun, 29 Jun 2025 15:22:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=82=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/script.js b/script.js index 2a905c5..f0157ef 100644 --- a/script.js +++ b/script.js @@ -10,16 +10,33 @@ const checkbox = document.getElementById('isLightTheme'); const html = document.getElementsByTagName('html')[0]; let scale = 1; // Начальный масштаб +function createTheme(nameTheme) { + if (nameTheme == 'dark') { + html.style.backgroundColor = '#1e1e1e'; + changeButtonsColor('#fff') + } else if (nameTheme == 'light') { + html.style.backgroundColor = '#fff'; + changeButtonsColor('#1e1e1e') + } +} + // Установление значения поля ввода из local storage и темы сайта window.onload = () => { // Установка темы сайта - const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); - if (darkModeMediaQuery.matches) { - html.style.backgroundColor = '#1e1e1e'; - changeButtonsColor('#fff') + let oldTheme = localStorage.getItem('theme') + if (oldTheme){ + createTheme(oldTheme) + + if (oldTheme == 'light') { + checkbox.checked = 1 + } } else { - html.style.backgroundColor = '#fff'; - changeButtonsColor('#1e1e1e') + const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); + if (darkModeMediaQuery.matches) { + createTheme('dark') + } else { + createTheme('light') + } } // Восстановление масштаба @@ -33,8 +50,6 @@ window.onload = () => { if (inputText) { textInput.value = inputText; generateQR() - } else { - localStorage.setItem('userText', '') } } @@ -49,12 +64,13 @@ function changeButtonsColor(color) { // Изменение темы сайта при клике checkbox.addEventListener('change', () => { - if (checkbox.checked) { - html.style.backgroundColor = '#fff'; - changeButtonsColor('#1e1e1e') + const theme = localStorage.getItem('theme') + if (theme == 'dark') { + createTheme('light') + localStorage.setItem('theme', 'light') } else { - html.style.backgroundColor = '#1e1e1e'; - changeButtonsColor('#fff') + createTheme('dark') + localStorage.setItem('theme', 'dark') } })