-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
220 lines (181 loc) · 6.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
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
const topBar = document.querySelector('#top-bar');
const exteriorColorSection = document.querySelector('#exterior-buttons');
const interiorColorSection = document.querySelector('#interior-buttons');
const exteriorImage = document.querySelector('#exterior-image');
const interiorImage = document.querySelector('#interior-image');
const wheelButtonsSection = document.querySelector('#wheel-buttons');
const performanceBtn = document.querySelector('#performance-btn');
const totalPriceElement = document.querySelector('#total-price');
const fullSelfDrivingCheckbox = document.querySelector(
'#full-self-driving-checkbox'
);
const accessoryCheckboxes = document.querySelectorAll(
'.accessory-form-checkbox'
);
const downPaymentElement = document.querySelector('#down-payment');
const monthlyPaymentElement = document.querySelector('#monthly-payment');
const basePrice = 52490;
let currentPrice = basePrice;
let selectedColor = 'Stealth Grey';
const selectedOptions = {
'Performance Wheels': false,
'Performance Package': false,
'Full Self-Drving': false,
};
const pricing = {
'Performance Wheels': 2500,
'Performance Package': 5000,
'Full Self-Driving': 8500,
Accessories: {
'Center Console Trays': 35,
Sunshade: 105,
'All-Weather Interior Liners': 225,
},
};
// Update total price in the UI
const updateTotalPrice = () => {
// Reset the current price to base price
currentPrice = basePrice;
// Performance Wheel Option
if (selectedOptions['Performance Wheels']) {
currentPrice += pricing['Performance Wheels'];
}
// Performance Package Option
if (selectedOptions['Performance Package']) {
currentPrice += pricing['Performance Package'];
}
// Full Self Driving Option
if (selectedOptions['Full Self-Driving']) {
currentPrice += pricing['Full Self-Driving'];
}
// Accessory Checkboxes
accessoryCheckboxes.forEach((checkbox) => {
// Extract the accessory label
const accessoryLabel = checkbox
.closest('label')
.querySelector('span')
.textContent.trim();
const accessoryPrice = pricing['Accessories'][accessoryLabel];
// Add to current price if accessory is selected
if (checkbox.checked) {
currentPrice += accessoryPrice;
}
});
// Update the total price in UI
totalPriceElement.textContent = `$${currentPrice.toLocaleString()}`;
updatePaymentBreakdown();
};
// Update payment breakdown based on current price
const updatePaymentBreakdown = () => {
// Calculate down payment
const downPayment = currentPrice * 0.1;
downPaymentElement.textContent = `$${downPayment.toLocaleString()}`;
// Calculate loan details (assuming 60-month loan and 3% interest rate)
const loanTermMonths = 60;
const interestRate = 0.03;
const loanAmount = currentPrice - downPayment;
// Monthly payment formula: P * (r(1+r)^n) / ((1+r)^n - 1)
const monthlyInterestRate = interestRate / 12;
const monthlyPayment =
(loanAmount *
(monthlyInterestRate *
Math.pow(1 + monthlyInterestRate, loanTermMonths))) /
(Math.pow(1 + monthlyInterestRate, loanTermMonths) - 1);
monthlyPaymentElement.textContent = `$${monthlyPayment
.toFixed(2)
.toLocaleString()}`;
};
// Handle Top Bar On Scroll
const handleScroll = () => {
const atTop = window.scrollY === 0;
topBar.classList.toggle('visible-bar', atTop);
topBar.classList.toggle('hidden-bar', !atTop);
};
// Image Mapping
const exteriorImages = {
'Stealth Grey': './images/model-y-stealth-grey.jpg',
'Pearl White': './images/model-y-pearl-white.jpg',
'Deep Blue': './images/model-y-deep-blue-metallic.jpg',
'Solid Black': './images/model-y-solid-black.jpg',
'Ultra Red': './images/model-y-ultra-red.jpg',
Quicksilver: './images/model-y-quicksilver.jpg',
};
const interiorImages = {
Dark: './images/model-y-interior-dark.jpg',
Light: './images/model-y-interior-light.jpg',
};
// Handle Color Selection
const handleColorButtonClick = (event) => {
let button;
if (event.target.tagName === 'IMG') {
button = event.target.closest('button');
} else if (event.target.tagName === 'BUTTON') {
button = event.target;
}
if (button) {
const buttons = event.currentTarget.querySelectorAll('button');
buttons.forEach((btn) => btn.classList.remove('btn-selected'));
button.classList.add('btn-selected');
// Change exterior image
if (event.currentTarget === exteriorColorSection) {
selectedColor = button.querySelector('img').alt;
updateExteriorImage();
}
// Change interior image
if (event.currentTarget === interiorColorSection) {
const color = button.querySelector('img').alt;
interiorImage.src = interiorImages[color];
}
}
};
// Update exterior image based on color and wheels
const updateExteriorImage = () => {
const performanceSuffix = selectedOptions['Performance Wheels']
? '-performance'
: '';
const colorKey =
selectedColor in exteriorImages ? selectedColor : 'Stealth Grey';
exteriorImage.src = exteriorImages[colorKey].replace(
'.jpg',
`${performanceSuffix}.jpg`
);
};
// Wheel Selection
const handleWheelButtonClick = (event) => {
if (event.target.tagName === 'BUTTON') {
const buttons = document.querySelectorAll('#wheel-buttons button');
buttons.forEach((btn) => btn.classList.remove('bg-gray-700', 'text-white'));
// Add selected styles to clicked button
event.target.classList.add('bg-gray-700', 'text-white');
selectedOptions['Performance Wheels'] =
event.target.textContent.includes('Performance');
updateExteriorImage();
updateTotalPrice();
}
};
// Performance Package Selection
const handlePerformanceButtonClick = () => {
const isSelected = performanceBtn.classList.toggle('bg-gray-700');
performanceBtn.classList.toggle('text-white');
// Update selected options
selectedOptions['Performance Package'] = isSelected;
updateTotalPrice();
};
// Full Self Driving Selection
const fullSelfDrivingChange = () => {
selectedOptions['Full Self-Driving'] = fullSelfDrivingCheckbox.checked;
updateTotalPrice();
};
// Handle Accessory Checkbox Listeners
accessoryCheckboxes.forEach((checkbox) => {
checkbox.addEventListener('change', () => updateTotalPrice());
});
// Initial Update Total Price
updateTotalPrice();
// Event Listeners
window.addEventListener('scroll', () => requestAnimationFrame(handleScroll));
exteriorColorSection.addEventListener('click', handleColorButtonClick);
interiorColorSection.addEventListener('click', handleColorButtonClick);
wheelButtonsSection.addEventListener('click', handleWheelButtonClick);
performanceBtn.addEventListener('click', handlePerformanceButtonClick);
fullSelfDrivingCheckbox.addEventListener('change', fullSelfDrivingChange);