-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (72 loc) · 2.79 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Smooth Scroll for Navigation Links (Top and Footer)
document.querySelectorAll('nav a, .footer-links a').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
// Check if the link is external (starts with "http")
if (href.startsWith("http")) {
// Allow external links to open normally
return;
}
// If it's an internal link, apply smooth scroll
e.preventDefault();
const targetId = href.substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
}
});
});
// Image Modal for Profile Picture
const profilePic = document.getElementById('profile-pic');
const modal = document.getElementById('image-modal');
const modalImg = document.getElementById('modal-image');
const closeBtn = document.querySelector('.close');
profilePic.addEventListener('click', () => {
modal.style.display = 'flex';
modalImg.src = profilePic.src;
});
closeBtn.addEventListener('click', () => {
modal.style.display = 'none';
});
window.addEventListener('click', (e) => {
if (e.target === modal) {
modal.style.display = 'none';
}
});
// Dark Mode Toggle
const darkModeToggle = document.getElementById('dark-mode-toggle');
// Check if dark mode is already enabled (stored in local storage)
if (localStorage.getItem('darkMode') === 'enabled') {
enableDarkMode();
darkModeToggle.checked = true; // Set the switch to checked if dark mode is enabled
}
// Function to enable dark mode
function enableDarkMode() {
document.body.classList.add('dark-mode');
document.querySelector('header').classList.add('dark-mode');
document.querySelector('nav').classList.add('dark-mode');
document.querySelector('footer').classList.add('dark-mode');
document.querySelectorAll('section').forEach(section => {
section.classList.add('dark-mode');
});
localStorage.setItem('darkMode', 'enabled'); // Save preference in local storage
}
// Function to disable dark mode
function disableDarkMode() {
document.body.classList.remove('dark-mode');
document.querySelector('header').classList.remove('dark-mode');
document.querySelector('nav').classList.remove('dark-mode');
document.querySelector('footer').classList.remove('dark-mode');
document.querySelectorAll('section').forEach(section => {
section.classList.remove('dark-mode');
});
localStorage.setItem('darkMode', 'disabled'); // Save preference in local storage
}
// Toggle dark mode when the switch is changed
darkModeToggle.addEventListener('change', () => {
if (darkModeToggle.checked) {
enableDarkMode();
} else {
disableDarkMode();
}
});