Enable the user's preferred theme

This commit is contained in:
Ilia Miheev
2025-02-18 21:41:28 +03:00
parent f7e4a8ac56
commit e51a4fe377
2 changed files with 24 additions and 3 deletions
+19 -2
View File
@@ -6,12 +6,21 @@ const delBtn = document.getElementById('del-qr-btn');
const textError = document.getElementById('error'); const textError = document.getElementById('error');
const zoomInBtn = document.getElementById('zoom-in-btn'); const zoomInBtn = document.getElementById('zoom-in-btn');
const zoomOutBtn = document.getElementById('zoom-out-btn'); const zoomOutBtn = document.getElementById('zoom-out-btn');
let scale = 1; // Начальный масштаб
const checkbox = document.getElementById('isLightTheme'); const checkbox = document.getElementById('isLightTheme');
const html = document.getElementsByTagName('html')[0]; const html = document.getElementsByTagName('html')[0];
let scale = 1; // Начальный масштаб
// Установление значения поля ввода из local storage // Установление значения поля ввода из local storage и темы сайта
window.onload = () => { window.onload = () => {
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
if (darkModeMediaQuery.matches) {
html.style.backgroundColor = '#1e1e1e';
changeButtonsColor('#fff')
} else {
html.style.backgroundColor = '#fff';
changeButtonsColor('#1e1e1e')
}
let inputText = localStorage.getItem('userText'); let inputText = localStorage.getItem('userText');
if (inputText) { if (inputText) {
textInput.value = inputText; textInput.value = inputText;
@@ -21,6 +30,7 @@ window.onload = () => {
} }
} }
// Изменение цвета текста в кнопках
function changeButtonsColor(color) { function changeButtonsColor(color) {
const buttons = document.getElementsByTagName('button'); const buttons = document.getElementsByTagName('button');
for (let index = 0; index < buttons.length; index++) { for (let index = 0; index < buttons.length; index++) {
@@ -29,6 +39,7 @@ function changeButtonsColor(color) {
} }
} }
// Изменение темы сайта при клике
checkbox.addEventListener('change', () => { checkbox.addEventListener('change', () => {
if (checkbox.checked) { if (checkbox.checked) {
html.style.backgroundColor = '#fff'; html.style.backgroundColor = '#fff';
@@ -46,12 +57,14 @@ function logMessage(message, color = 'red') {
textError.style.color = color; textError.style.color = color;
} }
// Функция для генерации QR-кода
function generateQR() { function generateQR() {
const userText = textInput.value.trim(); const userText = textInput.value.trim();
if (!userText) { if (!userText) {
logMessage('Введите текст для генерации QR-кода!'); logMessage('Введите текст для генерации QR-кода!');
return; return;
}; };
localStorage.setItem('userText', userText); localStorage.setItem('userText', userText);
qrCodeDiv.innerHTML = ''; qrCodeDiv.innerHTML = '';
QRCode.toCanvas(userText, (error, canvas) => { QRCode.toCanvas(userText, (error, canvas) => {
@@ -65,6 +78,7 @@ function generateQR() {
canvas.style.transform = `scale(${scale})`; canvas.style.transform = `scale(${scale})`;
canvas.style.transformOrigin = 'top'; canvas.style.transformOrigin = 'top';
}) })
const canvas = qrCodeDiv.querySelector('canvas'); const canvas = qrCodeDiv.querySelector('canvas');
let canvasWidth = canvas.getBoundingClientRect().width; let canvasWidth = canvas.getBoundingClientRect().width;
if (canvasWidth > window.innerWidth * 0.8) { if (canvasWidth > window.innerWidth * 0.8) {
@@ -118,6 +132,7 @@ zoomInBtn.addEventListener('click', () => {
logMessage('До генерации QR кода маштаб менять нельзя') logMessage('До генерации QR кода маштаб менять нельзя')
return return
} }
let canvasWidth = canvas.getBoundingClientRect().width; let canvasWidth = canvas.getBoundingClientRect().width;
if (canvasWidth + 0.1 < (window.innerWidth * 0.8)) { if (canvasWidth + 0.1 < (window.innerWidth * 0.8)) {
scale += 0.1; // Увеличить масштаб на 10% scale += 0.1; // Увеличить масштаб на 10%
@@ -134,10 +149,12 @@ zoomOutBtn.addEventListener('click', () => {
logMessage('До генерации QR кода маштаб менять нельзя') logMessage('До генерации QR кода маштаб менять нельзя')
return return
} }
let canvasWidth = canvas.getBoundingClientRect().width; let canvasWidth = canvas.getBoundingClientRect().width;
if (canvasWidth > window.innerWidth * 0.8) { if (canvasWidth > window.innerWidth * 0.8) {
scale = 1; scale = 1;
} }
if (scale > 0.5) { if (scale > 0.5) {
scale -= 0.1; // Уменьшить масштаб на 10% scale -= 0.1; // Уменьшить масштаб на 10%
chengeScale(scale) chengeScale(scale)
+5 -1
View File
@@ -9,7 +9,7 @@ html {
body { body {
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
margin: 0; margin: 20px;
text-align: center; text-align: center;
} }
@@ -41,6 +41,10 @@ textarea::selection {
color: white; color: white;
} }
input[type='checkbox'] {
accent-color: orangered;
}
button { button {
background-color: orangered; background-color: orangered;
color: #fff; color: #fff;