-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (68 loc) · 2.68 KB
/
script.js
File metadata and controls
80 lines (68 loc) · 2.68 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
// Smooth Scroll for Nav Links
document.querySelectorAll("nav ul li a").forEach(link => {
link.addEventListener("click", e => {
e.preventDefault();
const target = document.querySelector(link.getAttribute("href"));
if (target) {
target.scrollIntoView({
behavior: "smooth"
});
}
});
});
// Dark Mode Toggle
const toggleButton = document.getElementById("darkModeToggle");
toggleButton.addEventListener("click", () => {
document.body.classList.toggle("light-mode");
toggleButton.textContent = document.body.classList.contains("light-mode") ? "🌙" : "☀️";
// Save mode preference in local storage
localStorage.setItem("darkMode", document.body.classList.contains("light-mode") ? "light" : "dark");
});
// Preserve Dark Mode on Reload
if (localStorage.getItem("darkMode") === "light") {
document.body.classList.add("light-mode");
toggleButton.textContent = "🌙";
}
// Typing Effect
const text = "Web Developer | Designer | Freelancer";
let index = 0;
function typeEffect() {
const typingElement = document.getElementById("typing-text");
if (typingElement) {
typingElement.textContent = text.substring(0, index++);
if (index <= text.length) setTimeout(typeEffect, 150);
}
}
typeEffect();
// Contact Form Validation
document.getElementById("contactForm").addEventListener("submit", function (e) {
e.preventDefault();
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const message = document.getElementById("message").value.trim();
const formMessage = document.getElementById("formMessage");
// Basic Email Validation
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (name && email.match(emailPattern) && message) {
formMessage.textContent = "Message sent successfully!";
formMessage.style.color = "green";
// Clear the form after submission
document.getElementById("contactForm").reset();
} else {
formMessage.textContent = "Please fill all fields correctly!";
formMessage.style.color = "red";
}
});
// Scroll to Top Button
const scrollBtn = document.createElement("button");
scrollBtn.textContent = "⬆️";
scrollBtn.classList.add("scroll-top");
document.body.appendChild(scrollBtn);
window.addEventListener("scroll", () => {
scrollBtn.style.display = window.scrollY > 500 ? "block" : "none";
});
scrollBtn.addEventListener("click", () => window.scrollTo({ top: 0, behavior: "smooth" }));
// Prevent Console Errors if Elements Are Missing
window.addEventListener("error", (e) => {
console.warn("Caught an error:", e.message);
});