-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.js
77 lines (64 loc) · 2.17 KB
/
carousel.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
// preset upto 100 images from the ./img folder
let folder = `./img/Kenti`;
let image = folder
let images = []
for (let i = 0; i < 100; i++){
images.push(folder+i+ '.jpg')
}
// DOM setup for images
let carouselArr = document.getElementById('carousel')
const createCarouselImgGroup =(element)=>{
let liElement = document.createElement('li')
let imgElement = document.createElement('img')
liElement.setAttribute('class', 'carousel-item')
imgElement.setAttribute('src', element)
liElement.appendChild(imgElement)
carouselArr.appendChild(liElement)
return carouselArr
}
// New Test Code
const setCarousel = (start) => {
let finish = start + 4;
let carouselWindow = []
carouselWindow[0] = images[start]
carouselWindow[1] = images[start + 1]
carouselWindow[2] = images[start + 2]
carouselWindow[3] = images[start + 3]
carouselWindow[4] = images[start + 4]
return carouselWindow
}
let begin = 0;
let carouselWindow = setCarousel(begin)
carouselWindow.forEach((element)=>{createCarouselImgGroup(element)})
let leftArrow = document.getElementById('left')
let rightArrow = document.getElementById('right');
leftArrow.addEventListener('click',(event) => {
event.preventDefault();
begin -= 4
if (begin < 0 ){
begin = 0
leftArrow.setAttribute('class','carousel-item, left-arrow, left-arrow-zero')
} else {
leftArrow.removeAttribute("left-arrow-zero")
carouselArr.innerHTML = ""
let carouselWindow = setCarousel(begin)
carouselWindow.forEach((element)=>{createCarouselImgGroup(element)})
}
})
rightArrow.addEventListener('click',(event) => {
event.preventDefault()
carouselArr = document.getElementById('carousel')
begin += 4
if (begin >= 0 && begin + 5 < 31){
rightArrow.removeAttribute("right-arrow-zero")
carouselArr.innerHTML = ""
let carouselWindow = setCarousel(begin)
carouselWindow.forEach((element)=>{createCarouselImgGroup(element)})
} else {
rightArrow.setAttribute('class', 'carousel-item, right-arrow, right-arrow-zero')
begin = images.length - 5;
carouselArr.innerHTML = ""
let carouselWindow = setCarousel(begin)
carouselWindow.forEach((element)=>{createCarouselImgGroup(element)})
}
})