forked from sagar598/MR-Tour-and-Travel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (111 loc) · 4.62 KB
/
main.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
// Function to initialize the navbar
function initializeNavbar() {
const button = document.querySelector('[data-collapse-toggle]');
const menu = document.querySelector('#mobile-menu-2');
const navLinks = document.querySelectorAll('#mobile-menu-2 a');
const menuIcon = document.querySelector('.menu-icon');
const closeIcon = document.querySelector('.close-icon');
button.addEventListener('click', function () {
const expanded = this.getAttribute('aria-expanded') === 'true' || false;
this.setAttribute('aria-expanded', !expanded);
menu.classList.toggle('hidden');
menuIcon.classList.toggle('hidden');
closeIcon.classList.toggle('hidden');
});
navLinks.forEach(function (link) {
link.addEventListener('click', function () {
button.setAttribute('aria-expanded', 'false');
menu.classList.add('hidden');
menuIcon.classList.remove('hidden');
closeIcon.classList.add('hidden');
});
});
}
// Function to fetch and insert HTML content
function fetchAndInsert(url, placeholderId, callback) {
fetch(url)
.then(response => response.text())
.then(html => {
document.getElementById(placeholderId).innerHTML = html;
if (callback) {
callback(); // Execute the callback function after inserting HTML
}
})
.catch(error => console.error(`Error fetching ${url}:`, error));
}
// Load the header HTML
document.addEventListener("DOMContentLoaded", function () {
fetchAndInsert('/header.html', 'header-placeholder', function () {
// After loading the header, initialize the navbar
initializeNavbar();
// Add event listener to change navbar styles on scroll
const navbar = document.querySelector('header nav');
window.addEventListener('scroll', function () {
if (window.scrollY > 50) {
// Scrolled down, add styles
navbar.classList.remove('bg-transparent', 'text-white');
navbar.classList.add('bg-white', 'text-customBlue');
} else {
// At the top, remove styles
navbar.classList.remove('bg-white', 'text-customBlue');
navbar.classList.add('bg-transparent', 'text-white');
}
});
});
});
// Load the footer HTML
document.addEventListener("DOMContentLoaded", function () {
fetchAndInsert('/footer.html', 'footer-placeholder');
});
// Car Slider
document.addEventListener('DOMContentLoaded', function () {
const sliderContainer = document.getElementById('sliderContainer');
const slides = sliderContainer.querySelectorAll('.slide');
const slideWidth = slides[0].offsetWidth;
const slidesToShow = 3;
let currentIndex = 0;
// Duplicate slides for continuous loop
for (let i = 0; i < slidesToShow; i++) {
const clone = slides[i].cloneNode(true);
sliderContainer.appendChild(clone);
}
function updateSlider() {
sliderContainer.style.transition = 'transform 0.5s ease-in-out';
sliderContainer.style.transform = 'translateX(' + (-currentIndex * slideWidth) + 'px)';
}
function nextSlide() {
currentIndex++;
updateSlider();
}
function prevSlide() {
currentIndex--;
updateSlider();
}
function handleTransitionEnd() {
if (currentIndex >= slides.length) {
currentIndex = 0;
sliderContainer.style.transition = 'none';
sliderContainer.style.transform = 'translateX(' + (-currentIndex * slideWidth) + 'px)';
} else if (currentIndex < 0) {
currentIndex = slides.length - 1;
sliderContainer.style.transition = 'none';
sliderContainer.style.transform = 'translateX(' + (-currentIndex * slideWidth) + 'px)';
}
}
sliderContainer.addEventListener('transitionend', handleTransitionEnd);
function initSlider() {
setInterval(nextSlide, 3000); // Auto-advance slides every 3 seconds
}
document.getElementById('prevBtn').addEventListener('click', prevSlide);
document.getElementById('nextBtn').addEventListener('click', nextSlide);
initSlider();
});
// Preloader
document.addEventListener('DOMContentLoaded', function () {
// Show the spinner
document.querySelector('.spinner-container').style.display = 'block';
// Hide the spinner after 1 seconds
setTimeout(function() {
document.querySelector('.spinner-container').style.display = 'none';
}, 1000); // 1000 milliseconds = 1 seconds
});