-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
291 lines (245 loc) · 10.7 KB
/
script.js
File metadata and controls
291 lines (245 loc) · 10.7 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* The Scribe's Den - Main JavaScript
* Version: 1.0
* Description: Handles all interactive elements for The Scribe's Den website
*/
document.addEventListener('DOMContentLoaded', function() {
// Element references
const mobileMenuBtn = document.querySelector('.mobile-menu-btn');
const navLinks = document.querySelector('.nav-links');
const overlay = document.querySelector('.overlay');
const body = document.body;
const nav = document.getElementById('main-nav');
const scrollTopBtn = document.querySelector('.scroll-to-top');
const fadeElements = document.querySelectorAll('.fade-in');
// ================ MOBILE MENU TOGGLE ================
function toggleMenu() {
const isExpanded = mobileMenuBtn.getAttribute('aria-expanded') === 'true';
mobileMenuBtn.setAttribute('aria-expanded', !isExpanded);
navLinks.classList.toggle('active');
overlay.classList.toggle('active');
mobileMenuBtn.classList.toggle('active');
if (navLinks.classList.contains('active')) {
mobileMenuBtn.innerHTML = '<i class="fas fa-times"></i>';
body.style.overflow = 'hidden'; // Prevent scrolling when menu is open
} else {
mobileMenuBtn.innerHTML = '<i class="fas fa-bars"></i>';
body.style.overflow = ''; // Restore scrolling
}
}
// Add event listeners for menu
if (mobileMenuBtn && navLinks && overlay) {
mobileMenuBtn.addEventListener('click', toggleMenu);
overlay.addEventListener('click', toggleMenu);
// Close menu when clicking on links
const menuLinks = document.querySelectorAll('.nav-links a');
menuLinks.forEach(link => {
link.addEventListener('click', function() {
if (navLinks.classList.contains('active')) {
toggleMenu();
}
});
});
} else {
console.warn('Mobile menu elements not found. Check selectors: .mobile-menu-btn, .nav-links, .overlay');
}
// ================ STICKY NAVIGATION ================
let lastScrollTop = 0;
window.addEventListener('scroll', function() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// Add/remove scrolled class to change nav appearance
if (nav) {
if (scrollTop > 50) {
nav.classList.add('scrolled');
} else {
nav.classList.remove('scrolled');
}
}
// Show/hide scroll to top button
if (scrollTopBtn) {
if (scrollTop > 300) {
scrollTopBtn.classList.add('visible');
} else {
scrollTopBtn.classList.remove('visible');
}
}
lastScrollTop = scrollTop;
});
// ================ SCROLL TO TOP BUTTON ================
if (scrollTopBtn) {
scrollTopBtn.addEventListener('click', function() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
} else {
console.warn('Scroll-to-top button not found. Check selector: .scroll-to-top');
}
// ================ FADE-IN ANIMATIONS ================
if (fadeElements.length > 0) {
const fadeInObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = 1;
entry.target.style.transform = 'translateY(0)';
fadeInObserver.unobserve(entry.target);
}
});
}, {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
});
fadeElements.forEach(element => {
element.style.opacity = 0;
element.style.transform = 'translateY(20px)';
element.style.transition = 'opacity 0.5s ease-in-out, transform 0.5s ease-in-out';
fadeInObserver.observe(element);
});
}
// ================ TOUCH GESTURES FOR MOBILE ================
let touchStartX = 0;
let touchEndX = 0;
document.addEventListener('touchstart', e => {
touchStartX = e.changedTouches[0].screenX;
}, false);
document.addEventListener('touchend', e => {
touchEndX = e.changedTouches[0].screenX;
handleSwipe();
}, false);
function handleSwipe() {
const swipeThreshold = 100;
if (touchEndX - touchStartX > swipeThreshold && navLinks && !navLinks.classList.contains('active')) {
toggleMenu();
}
if (touchStartX - touchEndX > swipeThreshold && navLinks && navLinks.classList.contains('active')) {
toggleMenu();
}
}
// ================ ACCESSIBILITY SUPPORT ================
if (navLinks) {
const focusableElements = 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
const firstFocusableElement = navLinks.querySelectorAll(focusableElements)[0];
const focusableContent = navLinks.querySelectorAll(focusableElements);
const lastFocusableElement = focusableContent[focusableContent.length - 1];
document.addEventListener('keydown', function(e) {
const isTabPressed = e.key === 'Tab' || e.keyCode === 9;
const isEscPressed = e.key === 'Escape' || e.keyCode === 27;
if (isEscPressed && navLinks.classList.contains('active')) {
toggleMenu();
mobileMenuBtn.focus();
return;
}
if (!isTabPressed || !navLinks.classList.contains('active')) {
return;
}
if (e.shiftKey) {
if (document.activeElement === firstFocusableElement) {
lastFocusableElement.focus();
e.preventDefault();
}
} else {
if (document.activeElement === lastFocusableElement) {
firstFocusableElement.focus();
e.preventDefault();
}
}
});
}
// ================ FORM VALIDATION ================
const newsletterForm = document.querySelector('.newsletter-form');
if (newsletterForm) {
newsletterForm.addEventListener('submit', function(e) {
e.preventDefault();
const emailInput = this.querySelector('input[type="email"]');
const email = emailInput.value.trim();
if (isValidEmail(email)) {
// Simulate submission to info@thescribesden.com (frontend-only)
console.log(`Simulating email subscription: Sending ${email} to info@thescribesden.com`);
showFormMessage(newsletterForm, 'Thank you for subscribing! We’ll reach out from info@thescribesden.com.', 'success');
emailInput.value = '';
} else {
showFormMessage(newsletterForm, 'Please enter a valid email address.', 'error');
}
});
}
function isValidEmail(email) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email.toLowerCase());
}
function showFormMessage(form, message, type) {
const existingMessage = form.querySelector('.form-message');
if (existingMessage) {
existingMessage.remove();
}
const messageEl = document.createElement('div');
messageEl.className = `form-message ${type}`;
messageEl.textContent = message;
form.appendChild(messageEl);
setTimeout(() => {
messageEl.remove();
}, 4000);
}
// ================ TESTIMONIAL SLIDER ================
const testimonialSlider = document.querySelector('.testimonial-slider');
if (testimonialSlider && testimonialSlider.querySelectorAll('.testimonial-card').length > 1) {
let currentSlide = 0;
const testimonials = testimonialSlider.querySelectorAll('.testimonial-card');
const totalSlides = testimonials.length;
testimonials.forEach((testimonial, index) => {
if (index > 0) {
testimonial.style.display = 'none';
}
});
const dotsContainer = document.createElement('div');
dotsContainer.className = 'slider-dots';
for (let i = 0; i < totalSlides; i++) {
const dot = document.createElement('button');
dot.className = i === 0 ? 'dot active' : 'dot';
dot.setAttribute('aria-label', `Go to testimonial ${i + 1}`);
dot.addEventListener('click', () => goToSlide(i));
dotsContainer.appendChild(dot);
}
testimonialSlider.appendChild(dotsContainer);
setInterval(nextSlide, 6000);
function nextSlide() {
goToSlide((currentSlide + 1) % totalSlides);
}
function goToSlide(slideIndex) {
testimonials[currentSlide].style.display = 'none';
currentSlide = slideIndex;
testimonials[currentSlide].style.display = 'block';
document.querySelectorAll('.slider-dots .dot').forEach((dot, index) => {
dot.classList.toggle('active', index === currentSlide);
});
}
}
// ================ LAZY LOADING IMAGES ================
if ('loading' in HTMLImageElement.prototype) {
const images = document.querySelectorAll('img[loading="lazy"]');
images.forEach(img => {
if (img.dataset.src) {
img.src = img.dataset.src;
}
});
} else {
const lazyImages = document.querySelectorAll('img[data-src]');
if (lazyImages.length > 0) {
const lazyImageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.removeAttribute('data-src');
lazyImageObserver.unobserve(img);
}
});
});
lazyImages.forEach(image => {
lazyImageObserver.observe(image);
});
}
}
// ================ INITIALIZATION ================
console.log('The Scribe\'s Den scripts initialized');
});