-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (122 loc) · 3.95 KB
/
Copy pathscript.js
File metadata and controls
139 lines (122 loc) · 3.95 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Hamburger Menu
const hamburger = document.querySelector(".hamburgur");
const nav = document.querySelector("nav");
const bars = document.querySelector("#bars");
const cross = document.querySelector("#cross");
const navLinks = document.querySelectorAll("nav ul li a");
function toggleMenu() {
nav.classList.toggle("active");
if (nav.classList.contains("active")) {
bars.style.display = "none";
cross.style.display = "block";
} else {
bars.style.display = "block";
cross.style.display = "none";
}
}
if (hamburger) {
hamburger.addEventListener("click", toggleMenu);
}
// Close menu when clicking a link
navLinks.forEach(link => {
link.addEventListener("click", () => {
if (nav.classList.contains("active")) {
toggleMenu();
}
});
});
// Scroll Effect for Navbar
window.addEventListener("scroll", () => {
const header = document.querySelector("header");
if (window.scrollY > 50) {
header.classList.add("scrolled");
} else {
header.classList.remove("scrolled");
}
});
// Projects Generation
const projectsUrl = "./projectData.json";
async function fetchProjects() {
try {
const res = await fetch(projectsUrl);
const data = await res.json();
generateProjects(data.projects);
} catch (error) {
console.error("Error fetching projects:", error);
}
}
function generateProjects(projects) {
const projectContainer = document.querySelector(".project-body");
if (!projectContainer) return;
const projectHTML = projects.map(project => {
return `
<div class="project-card" data-aos="fade-up">
<div class="project-img-wrapper">
<img src="./img/projectIMG/${project.img}" alt="${project.name}" loading="lazy">
</div>
<div class="project-content">
<h3>${project.name}</h3>
<p>${project.description}</p>
<div style="display: flex; gap: 1rem;">
<a href="${project.link}" target="_blank" class="project-link">
Live Demo <i class="fa-solid fa-arrow-up-right-from-square"></i>
</a>
${project.code ? `
<a href="${project.code}" target="_blank" class="project-link" style="color: var(--text-muted)">
Code <i class="fa-brands fa-github"></i>
</a>
` : ''}
</div>
</div>
</div>
`;
}).join('');
projectContainer.innerHTML = projectHTML;
}
fetchProjects();
// Theme Toggle
const themeToggle = document.querySelector("#theme-toggle");
const body = document.querySelector("body");
const sunIcon = document.querySelector("#sun");
const moonIcon = document.querySelector("#moon");
function updateThemeIcon(isDark) {
if (isDark) {
sunIcon.style.display = "block";
moonIcon.style.display = "none"; // In dark mode, we typically show the SUN icon to switch to light, or vice versa?
// Let's stick to standard: Sun shows in Dark mode (to switch to light), Moon shows in Light mode (to switch to dark).
// Wait, usually: Light Mode -> Show Moon. Dark Mode -> Show Sun.
// Let's check my logic.
// If Dark Mode is active: Body has .dark-mode.
// Sun icon should be visible (to toggle light). Moon hidden.
} else {
// Light Mode
sunIcon.style.display = "none";
moonIcon.style.display = "block";
}
}
// Check Local Storage
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
body.classList.add("dark-mode");
updateThemeIcon(true);
} else {
updateThemeIcon(false);
}
if (themeToggle) {
themeToggle.addEventListener("click", () => {
body.classList.toggle("dark-mode");
const isDark = body.classList.contains("dark-mode");
updateThemeIcon(isDark);
localStorage.setItem("theme", isDark ? "dark" : "light");
});
}
// Preloader
window.addEventListener("load", () => {
const preloader = document.getElementById("preloader");
if (preloader) {
preloader.style.opacity = "0";
setTimeout(() => {
preloader.style.display = "none";
}, 500);
}
});