-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
56 lines (46 loc) · 1.82 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
'use strict';
// Element toggle function
const elementToggleFunc = function (elem) { elem.classList.toggle("active"); }
// Sidebar variables
const sidebar = document.querySelector("[data-sidebar]");
const sidebarBtn = document.querySelector("[data-sidebar-btn]");
// Sidebar toggle functionality for mobile
sidebarBtn.addEventListener("click", function () { elementToggleFunc(sidebar); });
// Page navigation variables
const navigationLinks = document.querySelectorAll("[data-nav-link]");
const pages = document.querySelectorAll("[data-page]");
// Add event listener to all navigation links
for (let i = 0; i < navigationLinks.length; i++) {
navigationLinks[i].addEventListener("click", function (event) {
event.preventDefault(); // Prevent default anchor behavior
// Get the target page from the link's text
const targetPage = this.innerText.toLowerCase();
// Toggle active class on the matching page and navigation link
for (let j = 0; j < pages.length; j++) {
if (pages[j].dataset.page === targetPage) {
pages[j].classList.add("active");
navigationLinks[j].classList.add("active");
} else {
pages[j].classList.remove("active");
navigationLinks[j].classList.remove("active");
}
}
// Scroll to the top of the page
window.scrollTo(0, 0);
});
}
// Contact form variables
const form = document.querySelector("[data-form]");
const formInputs = document.querySelectorAll("[data-form-input]");
const formBtn = document.querySelector("[data-form-btn]");
// Add event to all form input fields
for (let i = 0; i < formInputs.length; i++) {
formInputs[i].addEventListener("input", function () {
// Check form validation
if (form.checkValidity()) {
formBtn.removeAttribute("disabled");
} else {
formBtn.setAttribute("disabled", "");
}
});
}