-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (93 loc) · 3.59 KB
/
Copy pathscript.js
File metadata and controls
115 lines (93 loc) · 3.59 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
/* =========================
Abdul Hafeez Portfolio JS
- Theme toggle (dark/light)
- Mobile nav toggle
- Scroll reveal animations (IntersectionObserver)
- Mailto contact form
========================= */
(function () {
const root = document.documentElement;
// ---------- Theme ----------
const themeToggle = document.getElementById("themeToggle");
const themeIcon = document.getElementById("themeIcon");
function setTheme(theme) {
root.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
// Update icon
if (themeIcon) {
themeIcon.className = theme === "light" ? "fa-solid fa-moon" : "fa-solid fa-sun";
themeToggle?.setAttribute("aria-label", theme === "light" ? "Switch to dark mode" : "Switch to light mode");
}
}
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "light" || savedTheme === "dark") {
setTheme(savedTheme);
} else {
// Default: dark, but if user prefers light, use it.
const prefersLight = window.matchMedia && window.matchMedia("(prefers-color-scheme: light)").matches;
setTheme(prefersLight ? "light" : "dark");
}
themeToggle?.addEventListener("click", () => {
const current = root.getAttribute("data-theme") || "dark";
setTheme(current === "dark" ? "light" : "dark");
});
// ---------- Mobile Nav ----------
const navToggle = document.getElementById("navToggle");
const navLinks = document.getElementById("navLinks");
function closeNav() {
navLinks?.classList.remove("is-open");
navToggle?.setAttribute("aria-expanded", "false");
}
navToggle?.addEventListener("click", () => {
const isOpen = navLinks?.classList.toggle("is-open");
navToggle?.setAttribute("aria-expanded", isOpen ? "true" : "false");
});
// Close nav on link click (mobile)
navLinks?.querySelectorAll("a").forEach((a) => {
a.addEventListener("click", () => closeNav());
});
// Close nav when clicking outside (mobile)
document.addEventListener("click", (e) => {
const target = e.target;
if (!target) return;
const clickedInside = navLinks?.contains(target) || navToggle?.contains(target);
if (!clickedInside) closeNav();
});
// ---------- Reveal on scroll ----------
const revealEls = document.querySelectorAll(".reveal");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
observer.unobserve(entry.target); // animate once
}
});
},
{ threshold: 0.12 }
);
revealEls.forEach((el) => observer.observe(el));
// ---------- Footer year ----------
const yearEl = document.getElementById("year");
if (yearEl) yearEl.textContent = String(new Date().getFullYear());
// ---------- Contact form -> mailto ----------
const form = document.getElementById("contactForm");
form?.addEventListener("submit", (e) => {
e.preventDefault();
const name = document.getElementById("name")?.value?.trim() || "";
const subject = document.getElementById("subject")?.value?.trim() || "";
const message = document.getElementById("message")?.value?.trim() || "";
// Replace with your real email
const to = "abdulhavizeag@gmail.com";
const fullSubject = subject ? subject : "Portfolio Contact";
const bodyLines = [
`Name: ${name}`,
"",
message
];
const mailto = `mailto:${encodeURIComponent(to)}?subject=${encodeURIComponent(fullSubject)}&body=${encodeURIComponent(
bodyLines.join("\n")
)}`;
window.location.href = mailto;
});
})();