-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
49 lines (40 loc) · 1.94 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
// JavaScript can be used for adding interactive elements such as image sliders, animations, or form handling.
// Currently left empty as no specific interaction was described in the screenshot.
console.log("Car rental website loaded");
document.addEventListener("DOMContentLoaded", function () {
const fleetSection = document.getElementById("fleet");
function handleScroll() {
const fleetTop = fleetSection.getBoundingClientRect().top;
const windowHeight = window.innerHeight;
// Check if the fleet section is within view
if (fleetTop < windowHeight - 100) {
fleetSection.classList.add("visible");
// Remove the scroll event listener after the animation is triggered
window.removeEventListener("scroll", handleScroll);
}
}
window.addEventListener("scroll", handleScroll);
// Run handleScroll immediately in case the fleet section is already in view
handleScroll();
});
const buttons = document.querySelectorAll('.category-filters button');
const images = document.querySelectorAll('.car-gallery img');
const carGallery = document.querySelector('.car-gallery');
function filterImages(event, category) {
// Prevent any default action that might cause scrolling
event.preventDefault();
// Remove the 'active' class from all buttons
buttons.forEach(button => button.classList.remove('active'));
// Add the 'active' class to the clicked button
event.target.classList.add('active');
// Show images that match the selected category, hide others
images.forEach(image => {
if (image.getAttribute('data-category') === category || category === 'all') {
image.style.display = 'block';
} else {
image.style.display = 'none';
}
});
// Scroll to the car-gallery container to maintain view stability
carGallery.scrollIntoView({ behavior: 'smooth' });
}