-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageSlider.js
46 lines (32 loc) · 1.1 KB
/
imageSlider.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
const display = document.getElementById('output');
const btnTop = document.getElementById('btnLast');
const btnBottom = document.getElementById('btnNext');
// The list of images i use and save locally
const image = ['code1','code2','code3','code4','code5','code6','code7'];
// The unique index i use to define the exact image
let imageIndex = 0;
// The loadImage function starts by loading the page and provides us with an image
window.addEventListener('load', loadImage(image[imageIndex]));
function loadImage(image){
display.style.backgroundImage = `url('images/${image}.jpg')`;
display.style.transition = '2s';
}
// Top and bottom buttons are connect with functions
btnTop.addEventListener('click', previmage);
btnBottom.addEventListener('click', nextimage);
// Previous image
function previmage() {
imageIndex--;
if (imageIndex < 0) {
imageIndex = image.length - 1;
}
loadImage(image[imageIndex]);
}
// Next image
function nextimage() {
imageIndex++;
if (imageIndex > image.length - 1) {
imageIndex = 0;
}
loadImage(image[imageIndex]);
}