-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (135 loc) · 5.58 KB
/
script.js
File metadata and controls
172 lines (135 loc) · 5.58 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
// Wait until the DOM is fully loaded
document.addEventListener("DOMContentLoaded", function () {
// Enable smooth scrolling behavior for the whole page
document.documentElement.style.scrollBehavior = "smooth";
// Select all navbar links
const navLinks = document.querySelectorAll("nav a");
// Loop through each navbar link
navLinks.forEach(link => {
link.addEventListener("click", function (e) {
e.preventDefault(); // prevent instant jump
const targetId = this.getAttribute("href").substring(1); // remove '#' from href
const targetSection = document.getElementById(targetId);
// If the section exists, scroll smoothly to it
if (targetSection) {
targetSection.scrollIntoView({
behavior: "smooth",
block: "start"
});
}
});
});
});
// script.js
document.addEventListener('DOMContentLoaded', function () {
// ====== Price Calculation & Booking Form ======
const rideTypeSelect = document.getElementById('ride-type');
const distanceInput = document.getElementById('distance');
const estimatedPriceElement = document.getElementById('estimated-price');
const bookingForm = document.querySelector('.booking-form');
const prices = {
'mini': 12,
'auto': 10,
'suv': 18
};
function updatePrice() {
const rideType = rideTypeSelect.value;
const distance = parseFloat(distanceInput.value) || 0;
if (rideType && distance > 0) {
const price = prices[rideType] * distance;
// show as integer if whole, otherwise two decimals
estimatedPriceElement.textContent = Number.isInteger(price) ? price : price.toFixed(2);
} else {
estimatedPriceElement.textContent = '0';
}
}
if (rideTypeSelect) rideTypeSelect.addEventListener('change', updatePrice);
if (distanceInput) distanceInput.addEventListener('input', updatePrice);
if (bookingForm) {
bookingForm.addEventListener('submit', function (e) {
e.preventDefault();
const rideType = rideTypeSelect.value;
const distance = parseFloat(distanceInput.value) || 0;
if (!rideType) {
alert('Please select a ride type');
return;
}
if (distance <= 0) {
alert('Please enter a valid distance');
return;
}
const price = prices[rideType] * distance;
const rideTypeName = rideTypeSelect.options[rideTypeSelect.selectedIndex].text.split(' - ')[0];
alert(`Your estimated fare for ${rideTypeName} is ₹${Number.isInteger(price) ? price : price.toFixed(2)} for ${distance} km`);
});
}
// ====== Hamburger Menu Toggle (mobile) ======
const menuBars = document.getElementById('menu-bars');
const navbar = document.querySelector('.navbar');
if (menuBars && navbar) {
menuBars.addEventListener('click', function () {
navbar.classList.toggle('active');
});
}
// ====== Smooth Scrolling for Navbar Links (same-page) ======
// Use anchor hrefs like #home #rides #services #about #contact (see HTML step)
const navLinks = document.querySelectorAll('nav.navbar a');
// Optional: enable CSS smooth scrolling as a fallback
// (some browsers respect this; we still call scrollIntoView for control)
document.documentElement.style.scrollBehavior = 'smooth';
navLinks.forEach(link => {
link.addEventListener('click', function (e) {
// If link href starts with '#', handle smooth scroll
const href = this.getAttribute('href') || '';
if (href.startsWith('#')) {
e.preventDefault();
// close mobile navbar after click (if open)
if (navbar && navbar.classList.contains('active')) {
navbar.classList.remove('active');
}
const targetId = href.slice(1); // remove '#'
if (!targetId) return;
const targetEl = document.getElementById(targetId);
if (targetEl) {
// Use scrollIntoView with smooth behavior and slight offset for sticky header
const headerOffset = getStickyHeaderHeight(); // compute if sticky
const elementPosition = targetEl.getBoundingClientRect().top + window.pageYOffset;
const offsetPosition = elementPosition - headerOffset - 12; // 12px extra gap
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
} else {
// If the id is missing, fallback: jump to top
console.warn(`Target element with id="${targetId}" not found.`);
}
}
// else if href isn't a same-page anchor, default behavior will occur
});
});
// Utility to calculate sticky header height (if any)
function getStickyHeaderHeight() {
const header = document.querySelector('.header');
if (!header) return 0;
// If header is fixed/sticky, use its height; otherwise 0
const style = window.getComputedStyle(header);
const position = style.position;
if (position === 'fixed' || position === 'sticky') {
return header.offsetHeight;
}
// if not sticky, but visually overlaps due to other layout, still return its height as safe offset
return header.offsetHeight;
}
// ====== Optional: close any open mobile navbar when clicking outside ======
document.addEventListener('click', function (e) {
if (!navbar) return;
if (!navbar.classList.contains('active')) return;
// if click is inside navbar or on the menu button, do nothing
const clickedInsideNavbar = navbar.contains(e.target);
if (clickedInsideNavbar) return;
const clickedMenuBtn = menuBars && menuBars.contains(e.target);
if (clickedMenuBtn) return;
// otherwise close it
navbar.classList.remove('active');
});
});