-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathImage-Slider.js
33 lines (28 loc) · 912 Bytes
/
Image-Slider.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
// create an array of image sources;
let images = [
'image1.png', 'image2.png', 'image3.png','image4.png'
]
let i = 0;
//add initial image to canvas
let canvas = document.getElementById('canvas');
canvas.style.background = `url(./images/${images[0]})`
//add eventListeners to arrows
let arrows = document.querySelectorAll('.arrow');
arrows.forEach(function(arrow){
arrow.addEventListener('click', function(e){
if (e.target.id === "left"){
//check to see if at beginning of array
i--;
if (i < 0){
i = images.length -1;
}
canvas.style.background = `url(./images/${images[i]})`;
} else if (e.target.id === "right") {
i++;
if (i >= images.length ){
i = 0;
}
canvas.style.background = `url(./images/${images[i]})`;
}
})
});