-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (114 loc) · 3.56 KB
/
app.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const imagesArea = document.querySelector('.images');
const gallery = document.querySelector('.gallery');
const galleryHeader = document.querySelector('.gallery-header');
const searchBtn = document.getElementById('search-btn');
const sliderBtn = document.getElementById('create-slider');
const sliderContainer = document.getElementById('sliders');
// selected image
let sliders = [];
// If this key doesn't work
// Find the name in the url and go to their website
// to create your own api key
const KEY = '15674931-a9d714b6e9d654524df198e00&q';
// show images
const images = '';
const showImages = (images) => {
imagesArea.style.display = 'block';
gallery.innerHTML = '';
// show gallery title
galleryHeader.style.display = 'flex';
images.forEach(image => {
let div = document.createElement('div');
div.className = 'col-lg-3 col-md-4 col-xs-6 img-item mb-2';
div.innerHTML = ` <img class="img-fluid img-thumbnail" onclick=selectItem(event,"${image.webformatURL}") src="${image.webformatURL}" alt="${image.tags}">`;
gallery.appendChild(div)
})
}
const getImages = (query) => {
const url = `https://pixabay.com/api/?key=${KEY}=${query}&image_type=photo&pretty=true`
fetch(url)
.then(response => response.json())
.then(data => showImages(data.hits))
.catch(err => console.log(err))
}
let slideIndex = 0;
const selectItem = (event, img) => {
let element = event.target;
element.classList.add('added');
let item = sliders.indexOf(img);
if (item === -1) {
sliders.push(img);
} else {
alert('Hey, Already added !')
}
}
var timer
const createSlider = () => {
// check slider image length
if (sliders.length < 2) {
alert('Select at least 2 image.')
return;
}
// crate slider previous next area
sliderContainer.innerHTML = '';
const prevNext = document.createElement('div');
prevNext.className = "prev-next d-flex w-100 justify-content-between align-items-center";
prevNext.innerHTML = `
<span class="prev" onclick="changeItem(-1)"><i class="fas fa-chevron-left"></i></span>
<span class="next" onclick="changeItem(1)"><i class="fas fa-chevron-right"></i></span>
`;
sliderContainer.appendChild(prevNext)
document.querySelector('.main').style.display = 'block';
// hide image aria
imagesArea.style.display = 'none';
const duration = document.getElementById('duration').value || 1000;
if (duration < 0) {
alert("duration never be negative")
}
else if(duration > 0) {
sliders.forEach(slide => {
let item = document.createElement('div')
item.className = "slider-item";
item.innerHTML = `<img class="w-100"
src="${slide}"
alt="">`;
sliderContainer.appendChild(item)
})
}
changeSlide(0)
timer = setInterval(function () {
slideIndex++;
changeSlide(slideIndex);
}, duration);
}
// change slider index
const changeItem = index => {
changeSlide(slideIndex += index);
}
// change slide item
const changeSlide = (index) => {
const items = document.querySelectorAll('.slider-item');
if (index < 0) {
slideIndex = items.length - 1
index = slideIndex;
};
if (index >= items.length) {
index = 0;
slideIndex = 0;
}
items.forEach(item => {
item.style.display = "none"
})
items[index].style.display = "block"
}
searchBtn.addEventListener('click', function () {
document.querySelector('.main').style.display = 'none';
clearInterval(timer);
const search = document.getElementById('search');
getImages(search.value)
sliders.length = 0;
search.value='';
})
sliderBtn.addEventListener('click', function () {
createSlider()
})