-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (51 loc) · 1.96 KB
/
script.js
File metadata and controls
63 lines (51 loc) · 1.96 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
// Créer le contenu HTML pour un gâteau
function createCategoryHtml(categoryName, item) {
const container = document.createElement('div');
container.className = 'category-container';
const heading = document.createElement('h2');
heading.textContent = categoryName;
container.appendChild(heading);
const image = document.createElement('img');
image.src = item.image_link;
image.alt = item.name;
image.className = 'category-image';
container.appendChild(image);
const name = document.createElement('h3');
name.textContent = item.name;
container.appendChild(name);
const description = document.createElement('p');
description.textContent = item.description;
container.appendChild(description);
const link = document.createElement('a');
link.textContent = 'Découvrir plus';
link.href = `./${categoryName.toLowerCase().replace(/\s+/g, '-')}.php`; // Lien vers la page de la catégorie
link.className = 'category-link';
container.appendChild(link);
return container;
}
function displayCategories(data) {
const categoriesContainer = document.getElementById('categories-container');
for (const categoryName in data) {
const categoryItems = data[categoryName];
const categoryHtml = createCategoryHtml(categoryName, categoryItems[0]);
categoriesContainer.appendChild(categoryHtml);
}
}
// Charger les données du fichier JSON et afficher les catégories
function loadAndDisplayCategories() {
fetch('data.json')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
displayCategories(data);
})
.catch(error => {
console.error('Could not load categories:', error);
});
}
// Appeler cette fonction quand la fenêtre charge
window.addEventListener('load', loadAndDisplayCategories);