Skip to content

Commit 3b4e720

Browse files
committed
Code 2 - Class 15 / 5
1 parent a6ac80c commit 3b4e720

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="css/style.css">
7+
<title>Products</title>
8+
</head>
9+
<body>
10+
<section id="Products"></section>
11+
<script src="js/script.js"></script>
12+
</body>
13+
</html>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// const productContainer = document.querySelector("#Products");
2+
3+
// fetch("https://fakestoreapi.com/products")
4+
// .then((res) => res.json())
5+
// .then((data) => {
6+
// data.forEach((product) => {
7+
// productContainer.innerHTML += `
8+
// <div class="card">
9+
// <img src="${product.image}" alt="${product.title}">
10+
// <h2>${product.title}</h2>
11+
// <p>${product.price}</p>
12+
// <p>${product.description}</p>
13+
// </div>
14+
// `;
15+
// });
16+
// })
17+
// .catch(err => console.log("Error fetching products",err))
18+
19+
// const numbers = [1, 2, 3, 4, 5];
20+
// const doubled = numbers.map((num) => num * 2);
21+
// console.log("Original:", numbers);
22+
// console.log("Doubled:", doubled);
23+
24+
const productContainer = document.querySelector("#Products");
25+
26+
fetch("https://fakestoreapi.com/products")
27+
.then((res) => res.json())
28+
.then((data) => {
29+
const productCards = data.map(
30+
(product) =>
31+
`
32+
<div class="card">
33+
<img src="${product.image}" alt="${product.title}">
34+
<h2>${product.title}</h2>
35+
<p>${product.price}</p>
36+
<p>${product.description}</p>
37+
</div>
38+
`,
39+
);
40+
productContainer.innerHTML = productCards.join("");
41+
})
42+
.catch((err) => console.log("Error fetching products", err));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
const container = document.querySelector(".container");
4+
const nextBtn = document.querySelector("#next");
5+
const prevBtn = document.querySelector("#prev");
6+
7+
let imageEls = [];
8+
let currentIndex = 0;
9+
10+
// Fetch images from Picsum API
11+
fetch("https://picsum.photos/v2/list?page=2&limit=6")
12+
.then(res => {
13+
res.json()
14+
})
15+
.then(data => {
16+
// Create <img> elements dynamically
17+
data.forEach((img, index) => {
18+
let image = document.createElement("img");
19+
image.src = img.download_url;
20+
image.alt = "Slider image";
21+
if (index === 0) image.classList.add("show");
22+
container.appendChild(image);
23+
});
24+
25+
// Update NodeList after adding images
26+
imageEls = document.querySelectorAll(".container img");
27+
})
28+
.catch(err => console.error("Error fetching images:", err));
29+
30+
// Next Button
31+
nextBtn.addEventListener("click", () => {
32+
if (imageEls.length === 0) return;
33+
34+
imageEls[currentIndex].classList.remove("show");
35+
currentIndex = (currentIndex === imageEls.length - 1) ? 0 : currentIndex + 1;
36+
imageEls[currentIndex].classList.add("show");
37+
});
38+
39+
// Prev Button
40+
prevBtn.addEventListener("click", () => {
41+
if (imageEls.length === 0) return;
42+
43+
imageEls[currentIndex].classList.remove("show");
44+
currentIndex = (currentIndex === 0) ? imageEls.length - 1 : currentIndex - 1;
45+
imageEls[currentIndex].classList.add("show");
46+
});

0 commit comments

Comments
 (0)