-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
234 lines (201 loc) · 6.46 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
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
///////////////////////////////////////////////////////
// Project: EliteHomes - Luxury Real Estate Website
// Developer: JohnDev19
// GitHub: https://github.com/JohnDev19
////////////////////////////////////////////////////////
// Website Sections:
// 1. Navigation Bar
// 2. Hero Section
// 3. Partnerships Section
// 4. About Section
// 5. Why Choose Us Section
// 6. Performance Metrics Section
// 7. Properties Section
// 8. Contact Section
// 9. Footer Section
///////////////////////////////////////////////////////
// Technologies Used:
// - HTML5
// -CSS3
// - JavaScript
// - Bootstrap 5
// - Font Awesome
///////////////////////////////////////////////////////
document.addEventListener('DOMContentLoaded', () => {
const navbar = document.querySelector('.navbar');
const navbarToggle = document.querySelector('.navbar-toggle');
const navbarMenu = document.querySelector('.navbar-menu');
const contactForm = document.getElementById('contact-form');
const selectWrapper = document.querySelector('.custom-select-wrapper');
const selectInput = selectWrapper.querySelector('.custom-select');
const selectDropdown = selectWrapper.querySelector('.custom-select-dropdown');
const selectOptions = selectWrapper.querySelectorAll('.custom-select-option');
// scroll tracking
let lastScrollTop = 0;
// select dropdown
const initCustomSelect = () => {
selectInput.addEventListener('click', () => {
selectWrapper.classList.toggle('active');
});
// close dropdown
document.addEventListener('click', (event) => {
if (selectWrapper && !selectWrapper.contains(event.target)) {
selectWrapper.classList.remove('active');
}
});
// option selection handler
selectOptions.forEach(option => {
option.addEventListener('click', () => {
selectOptions.forEach(opt => opt.classList.remove('selected'));
option.classList.add('selected');
selectInput.textContent = option.textContent;
selectInput.setAttribute('data-selected-value', option.dataset.value);
selectWrapper.classList.remove('active');
});
});
};
// metric
const counters = document.querySelectorAll('.metric-number');
const animateCounter = (counter) => {
const target = parseInt(counter.getAttribute('data-target'));
let current = 0;
const updateCounter = () => {
const increment = target / 100;
if (current < target) {
current += increment;
counter.textContent = Math.round(current);
requestAnimationFrame(updateCounter);
} else {
counter.textContent = target;
}
};
updateCounter();
};
// counter Animation
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounter(entry.target);
observer.unobserve(entry.target);
}
});
});
counters.forEach(counter => {
observer.observe(counter);
});
// nav scroll behavior
const handleNavbarScroll = () => {
window.addEventListener('scroll', () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
if (scrollTop > lastScrollTop) {
// scrolling down
navbar.classList.add('hidden');
} else {
// scrolling up
navbar.classList.remove('hidden');
}
lastScrollTop = scrollTop <= 0 ? 0: scrollTop;
});
};
// mobile navigation
const initMobileNavigation = () => {
navbarToggle.addEventListener('click',
() => {
navbarToggle.classList.toggle('active');
navbarMenu.classList.toggle('active');
if (navbarMenu.classList.contains('active')) {
navbarMenu.style.animation = 'drop 0.5s ease forwards';
} else {
navbarMenu.style.animation = '';
}
});
document.querySelectorAll('.navbar-menu a').forEach(link => {
link.addEventListener('click', () => {
navbarToggle.classList.remove('active');
navbarMenu.classList.remove('active');
navbarMenu.style.animation = '';
});
});
};
// smooth scrolling - anchor links
const initSmoothScroll = () => {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
};
// form validation and Submission (this is not actual)
const handleFormSubmission = () => {
contactForm.addEventListener('submit',
(e) => {
e.preventDefault();
const formData = new FormData(contactForm);
const name = formData.get('name');
const email = formData.get('email');
const message = formData.get('message');
if (!name || !email || !message) {
alert('Please fill in all required fields.');
return;
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
alert('Please enter a valid email address.');
return;
}
alert('Thank you for your inquiry! We will contact you shortly.');
contactForm.reset();
});
};
// scroll animation
const initScrollAnimations = () => {
const animateOnScroll = (entries,
observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate');
observer.unobserve(entry.target);
}
});
};
const observer = new IntersectionObserver(animateOnScroll,
{
root: null,
threshold: 0.1
});
document.querySelectorAll('.animate-on-scroll').forEach(item => {
observer.observe(item);
});
};
const initGoToTop = () => {
const goToTopBtn = document.getElementById('goToTop');
window.addEventListener('scroll',
() => {
if (window.pageYOffset > window.innerHeight * 0.5) {
goToTopBtn.classList.add('visible');
} else {
goToTopBtn.classList.remove('visible');
}
});
goToTopBtn.addEventListener('click',
() => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
};
const init = () => {
initCustomSelect();
handleNavbarScroll();
initMobileNavigation();
initSmoothScroll();
handleFormSubmission();
initScrollAnimations();
initGoToTop();
};
init();
});