|
1 | | -document.addEventListener('DOMContentLoaded', () => { |
| 1 | +document.addEventListener('DOMContentLoaded', function() { |
| 2 | + // SVG icons |
| 3 | + var sun = '<svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="3.5"/><line x1="8" y1="1" x2="8" y2="3"/><line x1="8" y1="13" x2="8" y2="15"/><line x1="1" y1="8" x2="3" y2="8"/><line x1="13" y1="8" x2="15" y2="8"/><line x1="3.05" y1="3.05" x2="4.46" y2="4.46"/><line x1="11.54" y1="11.54" x2="12.95" y2="12.95"/><line x1="3.05" y1="12.95" x2="4.46" y2="11.54"/><line x1="11.54" y1="4.46" x2="12.95" y2="3.05"/></svg>'; |
| 4 | + var moon = '<svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M13.5 8.5a5.5 5.5 0 0 1-7-7 5.5 5.5 0 1 0 7 7z"/></svg>'; |
| 5 | + |
2 | 6 | // Theme toggle |
3 | | - const toggle = document.getElementById('theme-toggle'); |
4 | | - const update = () => { |
5 | | - const isDark = document.documentElement.getAttribute('data-theme') === 'dark'; |
6 | | - toggle.textContent = isDark ? '☀️' : '🌙'; |
7 | | - }; |
8 | | - |
9 | | - toggle.addEventListener('click', () => { |
10 | | - const isDark = document.documentElement.getAttribute('data-theme') === 'dark'; |
11 | | - const next = isDark ? 'light' : 'dark'; |
| 7 | + var toggle = document.getElementById('theme-toggle'); |
| 8 | + if (!toggle) return; |
| 9 | + |
| 10 | + function isDark() { |
| 11 | + return document.documentElement.getAttribute('data-theme') === 'dark'; |
| 12 | + } |
| 13 | + |
| 14 | + function updateIcon() { |
| 15 | + toggle.innerHTML = isDark() ? sun : moon; |
| 16 | + } |
| 17 | + |
| 18 | + toggle.addEventListener('click', function() { |
| 19 | + var next = isDark() ? 'light' : 'dark'; |
12 | 20 | document.documentElement.setAttribute('data-theme', next); |
13 | 21 | localStorage.setItem('theme', next); |
14 | | - update(); |
| 22 | + updateIcon(); |
15 | 23 | }); |
16 | 24 |
|
17 | | - update(); |
| 25 | + updateIcon(); |
18 | 26 |
|
19 | 27 | // Back to top button |
20 | | - const topBtn = document.createElement('a'); |
| 28 | + var topBtn = document.createElement('a'); |
21 | 29 | topBtn.href = '#'; |
22 | 30 | topBtn.className = 'back-to-top'; |
23 | 31 | topBtn.setAttribute('aria-label', 'Back to top'); |
24 | | - topBtn.textContent = '↑'; |
| 32 | + topBtn.textContent = '\u2191'; |
25 | 33 | document.body.appendChild(topBtn); |
26 | 34 |
|
27 | | - window.addEventListener('scroll', () => { |
| 35 | + window.addEventListener('scroll', function() { |
28 | 36 | if (window.scrollY > 400) { |
29 | 37 | topBtn.classList.add('visible'); |
30 | 38 | } else { |
31 | 39 | topBtn.classList.remove('visible'); |
32 | 40 | } |
33 | 41 | }, { passive: true }); |
34 | 42 |
|
35 | | - topBtn.addEventListener('click', (e) => { |
| 43 | + topBtn.addEventListener('click', function(e) { |
36 | 44 | e.preventDefault(); |
37 | 45 | window.scrollTo({ top: 0, behavior: 'smooth' }); |
38 | 46 | }); |
|
0 commit comments