-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathproductSlider.html
105 lines (94 loc) · 3.02 KB
/
productSlider.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Product Slider</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css"
integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="productSlider.css" />
</head>
<body>
<div id="wrapper">
<div id="slider">
<div class="loader">
<h3>Loading...</h3>
</div>
<div class="content">
<div class="left">
<img alt="Product Image" />
</div>
<div class="right">
<h3 id="title"></h3>
<p id="desc"></p>
<p id="price">
<i class="fa-solid fa-dollar"></i>
<span></span>
</p>
</div>
</div>
<div class="nav">
<button disabled id="prev">
<i class="fa-solid fa-chevron-left"></i>
</button>
<button id="next"><i class="fa-solid fa-chevron-right"></i></button>
</div>
</div>
</div>
<script>
const btns = document.querySelectorAll("button");
const loader = document.querySelector(".loader");
const content = document.querySelector(".content");
let page = 1;
// function fetchData(page) {
// fetch("https://fakestoreapi.com/products/" + page)
// .then((response) => response.json())
// .then((result) => {
// showData(result);
// })
// .catch((err) => console.log(err));
// }
// fetchData(page);
async function fetchData(page) {
const response = await fetch(
"https://fakestoreapi.com/products/" + page
);
const result = await response.json();
showData(result);
}
function showData(data) {
toggleDisplay("none", "flex")
const img = document.querySelector("img");
const title = document.querySelector("#title");
const desc = document.querySelector("#desc");
const price = document.querySelector("#price span");
img.src = data.image;
title.innerText = data.title;
desc.innerText = data.description;
price.innerText = data.price;
btns[0].disabled = page > 1 ? false : true;
}
function toggleDisplay(loaderDisplay, contentDisplay) {
loader.style.display = loaderDisplay;
content.style.display = contentDisplay;
}
btns.forEach((btn) => {
btn.onclick = () => {
if (btn.id === "prev") {
if (page > 1) page--;
} else {
if (page < 20) page++;
}
toggleDisplay("flex", "none");
fetchData(page);
};
});
fetchData(page);
</script>
</body>
</html>