-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
76 lines (70 loc) · 2.1 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
let timer;
let removeFirstImg;
/*---------Fetch Dog Breeds-------*/
async function dogs() {
try {
const response = await fetch("https://dog.ceo/api/breeds/list/all");
const data = await response.json();
dogBreedList(data.message);
} catch (e) {
console.log(`There was a Problem with the Network`);
}
}
dogs();
/*----------Create a dropdown----------*/
function dogBreedList(breedList) {
document.getElementById("dogBreed").innerHTML = `
<select onchange='loadBreed(this.value)'>
<option>Choose a Dog Breed</option>
${Object.keys(breedList)
.map(function (breed) {
return `<option>${breed}</option>`;
})
.join("")}
</select>
`;
}
/*---------------Get a breed------------*/
async function loadBreed(breed) {
if (breed != "Choose a Dog Breed") {
const response = await fetch(`https://dog.ceo/api/breed/${breed}/images`);
const data = await response.json();
dogSlideShow(data.message);
}
}
/*--------------Breed Slideshow-------------------*/
function dogSlideShow(images) {
let currPosition = 0;
clearInterval(timer);
clearTimeout(removeFirstImg);
if (images.length > 1) {
document.getElementById("slideShow").innerHTML = `
<div class="slide" style="background-image: url('${images[0]}')"></div>
<div class="slide" style="background-image: url('${images[1]}')"></div>
`;
currPosition += 2;
if (images.length == 2) currPosition = 0;
timer = setInterval(nextSlide, 3000);
} else {
document.getElementById("slideShow").innerHTML = `
<div class="slide" style="background-image: url('${images[0]}')"></div>
<div class="slide"></div>
`;
}
function nextSlide() {
document
.getElementById("slideShow")
.insertAdjacentHTML(
"beforeend",
`<div class="slide" style="background-image: url('${images[currPosition]}')"></div>`
);
removeFirstImg = setTimeout(function () {
document.querySelector(".slide").remove();
}, 1000);
if (currPosition + 1 >= images.length) {
currPosition = 0;
} else {
currPosition++;
}
}
}