-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
185 lines (155 loc) · 6.18 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
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
// Hamburger Menu
document.addEventListener('DOMContentLoaded', () => {
const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const body = document.body;
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
navLinks.classList.toggle('active');
body.style.overflow = body.style.overflow === 'hidden' ? '' : 'hidden';
});
// Close menu when clicking links
document.querySelectorAll('.nav-links a').forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
navLinks.classList.remove('active');
body.style.overflow = '';
});
});
});
// Project Carousel
class ProjectCarousel {
constructor() {
this.slider = document.querySelector('.project-slider');
this.slides = document.querySelectorAll('.project-slide');
this.dotsContainer = document.querySelector('.carousel-dots');
this.prevBtn = document.querySelector('.carousel-btn.prev');
this.nextBtn = document.querySelector('.carousel-btn.next');
this.currentSlide = 0;
this.isTransitioning = false; // Prevent rapid clicking
this.init();
}
init() {
// Create dots
this.slides.forEach((_, index) => {
const dot = document.createElement('div');
dot.classList.add('dot');
if (index === 0) {
dot.classList.add('active');
this.slides[0].classList.add('active');
}
dot.addEventListener('click', () => this.goToSlide(index));
this.dotsContainer.appendChild(dot);
});
// Add button listeners
this.prevBtn?.addEventListener('click', () => this.prevSlide());
this.nextBtn?.addEventListener('click', () => this.nextSlide());
// Add touch support
this.touchStartX = 0;
this.touchEndX = 0;
this.slider.addEventListener('touchstart', (e) => {
this.touchStartX = e.changedTouches[0].screenX;
});
this.slider.addEventListener('touchend', (e) => {
this.touchEndX = e.changedTouches[0].screenX;
if (this.touchStartX - this.touchEndX > 50) this.nextSlide();
if (this.touchStartX - this.touchEndX < -50) this.prevSlide();
});
// Auto play with longer interval
this.startAutoPlay();
}
startAutoPlay() {
this.autoPlayInterval = setInterval(() => this.nextSlide(), 6000); // Longer interval (6 seconds)
// Pause on hover
this.slider.addEventListener('mouseenter', () => {
clearInterval(this.autoPlayInterval);
});
this.slider.addEventListener('mouseleave', () => {
this.startAutoPlay();
});
}
updateSlides() {
this.slides.forEach((slide, index) => {
slide.classList.toggle('active', index === this.currentSlide);
});
}
updateDots() {
const dots = this.dotsContainer.querySelectorAll('.dot');
dots.forEach((dot, index) => {
dot.classList.toggle('active', index === this.currentSlide);
});
}
goToSlide(index) {
if (this.isTransitioning) return; // Prevent rapid transitions
this.isTransitioning = true;
this.currentSlide = index;
this.slider.style.transform = `translateX(-${index * 100}%)`;
this.updateDots();
this.updateSlides();
// Reset transition lock after animation completes
setTimeout(() => {
this.isTransitioning = false;
}, 800); // Match this with CSS transition duration
}
nextSlide() {
if (!this.isTransitioning) {
this.currentSlide = (this.currentSlide + 1) % this.slides.length;
this.goToSlide(this.currentSlide);
}
}
prevSlide() {
if (!this.isTransitioning) {
this.currentSlide = (this.currentSlide - 1 + this.slides.length) % this.slides.length;
this.goToSlide(this.currentSlide);
}
}
}
// Initialize carousel when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
if (document.querySelector('.project-carousel')) {
new ProjectCarousel();
}
});
// Contact Form Handling
document.addEventListener('DOMContentLoaded', () => {
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = contactForm.querySelector('.submit-btn');
const btnText = submitBtn.querySelector('.btn-text');
// Show loading state
submitBtn.classList.add('loading');
try {
const response = await fetch(contactForm.action, {
method: 'POST',
body: new FormData(contactForm),
headers: {
'Accept': 'application/json'
}
});
if (response.ok) {
// Show success message
const successMessage = document.createElement('div');
successMessage.className = 'success-message';
successMessage.textContent = 'Message sent successfully!';
contactForm.appendChild(successMessage);
// Reset form
contactForm.reset();
// Remove success message after 5 seconds
setTimeout(() => {
successMessage.remove();
}, 5000);
} else {
throw new Error('Failed to send message');
}
} catch (error) {
console.error('Error:', error);
alert('Failed to send message. Please try again.');
} finally {
// Reset button state
submitBtn.classList.remove('loading');
}
});
}
});