-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
154 lines (133 loc) · 4.24 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Main JavaScript module
const App = {
init() {
this.setupMobileMenu();
this.setupSmoothScroll();
this.setupNavbarScroll();
this.setupForms();
this.setupLazyLoading();
this.setupScrollReveal();
this.setupBackToTop();
},
setupMobileMenu() {
const menuBtn = document.querySelector('.mobile-menu-btn');
const navLinks = document.querySelector('.nav-links');
const navLinksItems = document.querySelectorAll('.nav-links a');
// Toggle menu on button click
menuBtn?.addEventListener('click', () => {
this.toggleMobileMenu(navLinks);
});
// Close menu when clicking on links
navLinksItems.forEach((link) => {
link.addEventListener('click', () => {
this.toggleMobileMenu(navLinks, false);
});
});
},
toggleMobileMenu(navLinks, force) {
const isActive =
force !== undefined ? force : !navLinks.classList.contains('active');
navLinks.classList.toggle('active', isActive);
document.body.style.overflow = isActive ? 'hidden' : '';
},
setupSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener('click', (e) => {
e.preventDefault();
const target = document.querySelector(anchor.getAttribute('href'));
target?.scrollIntoView({ behavior: 'smooth' });
});
});
},
setupNavbarScroll() {
const navbar = document.querySelector('.navbar');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.scrollY;
navbar.style.background =
currentScroll > 50 ? 'rgba(26,26,26,0.9)' : 'var(--dark)';
navbar.style.transform =
currentScroll > lastScroll ? 'translateY(-100%)' : 'translateY(0)';
lastScroll = currentScroll;
});
},
setupForms() {
const forms = document.querySelectorAll('form');
forms.forEach((form) => {
const inputs = form.querySelectorAll('input, textarea, select');
inputs.forEach((input) => {
input.addEventListener('focus', () => {
input.parentElement.classList.add('focused');
});
input.addEventListener('blur', () => {
input.parentElement.classList.remove('focused');
});
});
form.addEventListener('submit', this.handleFormSubmit.bind(this));
});
},
async handleFormSubmit(e) {
e.preventDefault();
const form = e.target;
form.classList.add('loading');
try {
// Simulate form submission
await new Promise((resolve) => setTimeout(resolve, 1000));
form.reset();
alert('Form submitted successfully!');
} catch (error) {
alert('Error submitting form. Please try again.');
} finally {
form.classList.remove('loading');
}
},
setupLazyLoading() {
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.add('loaded');
observer.unobserve(img);
}
});
});
images.forEach((img) => imageObserver.observe(img));
},
setupScrollReveal() {
const reveals = document.querySelectorAll('.reveal');
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add('active');
}
});
},
{ threshold: 0.1 },
);
reveals.forEach((element) => observer.observe(element));
},
setupBackToTop() {
const backToTop = document.createElement('div');
backToTop.classList.add('back-to-top');
backToTop.innerHTML = '<i class="fas fa-arrow-up"></i>';
document.body.appendChild(backToTop);
window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
backToTop.classList.add('visible');
} else {
backToTop.classList.remove('visible');
}
});
backToTop.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
});
},
};
// Initialize app when DOM is ready
document.addEventListener('DOMContentLoaded', () => App.init());