-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
75 lines (65 loc) · 1.89 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
let timer;
let deleteFirstphotoDelay;
async function start(){
try{
const response=await fetch("https://dog.ceo/api/breeds/list/all");
const data=await response.json();
createList(data.message);
}
catch(error){
console.log("error ",error);
}
}
start();
function createList(breedList){
document.getElementById("breed").innerHTML=`
<select onchange="loadbyBreed(this.value)">
<option>Choose a dog breed</option>
${Object.keys(breedList).map(function(breed){
return `<option>${breed}</option>`;
}).join('')};
</select>
`;
}
async function loadbyBreed(breed){
if(breed!=="Choose a dog breed"){
const response=await fetch(`https://dog.ceo/api/breed/${breed}/images`);
const data=await response.json();
createSlideShow(data.message);
}
}
function createSlideShow(images){
let Pos=0;
clearInterval(timer);
clearTimeout(deleteFirstphotoDelay);
const imgSpace=document.querySelector('#slideshow');
if(images.length>1){
imgSpace.innerHTML=`
<div class="slide" style="background-image: url('${images[0]}')"></div>
<div class="slide" style="background-image: url('${images[1]}')"></div>
`;
Pos+=2;
if(images.length==2){
Pos=0;
}
timer=setInterval(nextSlide,3000);
}
else{
imgSpace.innerHTML=`
<div class="slide" style="background-image: url('${images[0]}')"></div>
<div class="slide"></div>
`;
}
function nextSlide(){
imgSpace.insertAdjacentHTML("beforeend", ` <div class="slide" style="background-image: url('${images[Pos]}')"></div>`);
deleteFirstphotoDelay= setTimeout(function(){
document.querySelector(".slide").remove();
}, 1000);
if(Pos+1>=images.length){
Pos=0;
}
else{
Pos++;
}
}
}