-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (59 loc) · 2.01 KB
/
index.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
let timer
let deleteFirstPhotoDelay
async function start(){
try{
const respose = await fetch('https://dog.ceo/api/breeds/list/all');
const data = await respose.json();
breedList(data.message)
}catch (e) {
console.log("there was a problem fetching the bleed of dogs.");
}
}
start();
function breedList(breedItems){
document.getElementById('dogbleed').innerHTML = `
<select onchange="loadBreed(this.value)" class="dog1">
<option>CHOOSE A DOG BREED</option>
${Object.keys(breedItems).map(function(breed){
return `<option>${breed}</option>`
}).join('')}
</select>
`
}
async function loadBreed(breed){
if (breed != "CHOOSE A DOG BLEED") {
const respose =await fetch(`https://dog.ceo/api/breed/${breed}/images`)
const data = await respose.json();
createSlideShow(data.message)
}
}
function createSlideShow(images){
let currentPosition = 0
clearInterval(timer)
clearTimeout(deleteFirstPhotoDelay)
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>
`
currentPosition +=2
if(images.length == 2) currentPosition = 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[currentPosition]}')"></div>`)
deleteFirstPhotoDelay = setTimeout(function(){
document.querySelector(".slide").remove()
},1000)
if(currentPosition + 1 >= images.length){
currentPosition = 0
}else{
currentPosition++
}
}
}