-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (43 loc) · 1.83 KB
/
script.js
File metadata and controls
64 lines (43 loc) · 1.83 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
document.addEventListener('DOMContentLoaded', () => {
const searchView = document.getElementById('search-view');
const detailView = document.getElementById('detail-view');
const backButton = document.getElementById('back-button');
const recipeCards = document.querySelectorAll('.recipe-card');
// Detail view elements to update
const detailImage = document.getElementById('detail-image');
const detailTitle = document.getElementById('detail-title');
const detailTime = document.getElementById('detail-time');
const detailCals = document.getElementById('detail-cals');
// Show Detail View
function showDetail(card) {
// Extract data from the card
const imageSrc = card.querySelector('.recipe-image').src;
const title = card.querySelector('.recipe-title').textContent;
const time = card.querySelector('.recipe-time').textContent;
const cals = card.querySelector('.recipe-cals').textContent.split(' ')[0]; // Just the number
// Populate detail view
detailImage.style.backgroundImage = `url("${imageSrc}")`;
detailTitle.textContent = title;
detailTime.textContent = time;
detailCals.textContent = cals;
// Transition
searchView.classList.add('hidden');
detailView.classList.remove('hidden');
window.scrollTo(0, 0);
}
// Show Search View
function showSearch() {
detailView.classList.add('hidden');
searchView.classList.remove('hidden');
}
// Event Listeners
recipeCards.forEach(card => {
card.addEventListener('click', () => showDetail(card));
});
if (backButton) {
backButton.addEventListener('click', (e) => {
e.preventDefault(); // Prevent default link behavior if it's an anchor
showSearch();
});
}
});