diff --git a/darkMode.js b/darkMode.js index 5da2cc64..7910040d 100644 --- a/darkMode.js +++ b/darkMode.js @@ -1,22 +1,19 @@ -const checkbox = document.getElementById('checkbox') -const modeLabel = document.getElementById('mode-label') +// Get the toggle checkbox +const toggleSwitch = document.getElementById('toggleDarkMode'); -// Check if dark mode is already enabled -if (localStorage.getItem('theme') === 'dark') { - document.documentElement.setAttribute('data-theme', 'dark') - checkbox.checked = true - modeLabel.textContent = 'Dark Mode' +// Check if user has previously selected dark mode +if (localStorage.getItem('darkMode') === 'enabled') { + document.body.classList.add('dark-mode'); + toggleSwitch.checked = true; } -// Add event listener for toggle switch -checkbox.addEventListener('change', () => { - if (checkbox.checked) { - document.documentElement.setAttribute('data-theme', 'dark') - localStorage.setItem('theme', 'dark') - modeLabel.textContent = 'Dark Mode' +// Event listener for the dark mode toggle +toggleSwitch.addEventListener('change', () => { + if (toggleSwitch.checked) { + document.body.classList.add('dark-mode'); + localStorage.setItem('darkMode', 'enabled'); } else { - document.documentElement.setAttribute('data-theme', 'light') - localStorage.setItem('theme', 'light') - modeLabel.textContent = 'Light Mode' + document.body.classList.remove('dark-mode'); + localStorage.setItem('darkMode', 'disabled'); } -}) \ No newline at end of file +}); diff --git a/darkmode.css b/darkmode.css new file mode 100644 index 00000000..321bbf9c --- /dev/null +++ b/darkmode.css @@ -0,0 +1,61 @@ +/* Default light mode styles */ +body { + font-family: Arial, sans-serif; + background-color: #ffffff; + color: #000000; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.container { + text-align: center; + padding: 50px}; + +/* Toggle switch styling */ +.switch { + position: relative; + display: inline-block; + width: 60px; + height: 34px; +} + +.switch input { + display: none; +} + +.slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: #ccc; + transition: .4s; + border-radius: 34px; +} + +.slider:before { + position: absolute; + content: ""; + height: 26px; + width: 26px; + left: 4px; + bottom: 4px; + background-color: white; + transition: .4s; + border-radius: 50%; +} + +input:checked + .slider { + background-color: #2196F3; +} + +input:checked + .slider:before { + transform: translateX(26px); +} + +/* Dark mode styles */ +body.dark-mode { + background-color: #121212; + color: #ffffff; +} diff --git a/index.html b/index.html index 9b1b9141..c53707ec 100644 --- a/index.html +++ b/index.html @@ -10,6 +10,8 @@ + +
@@ -51,10 +53,14 @@