-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslide-stories.js
72 lines (56 loc) · 1.6 KB
/
slide-stories.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
class SlideStories {
constructor(id){
this.slide = document.querySelector(`[data-slide="${id}"]`);
this.active = 0;
this.init();
}
activeSlide(index){
this.active = index;
this.items.forEach(item => item.classList.remove('active'));
this.items[index].classList.add('active');
this.thumbItems.forEach(item => item.classList.remove('active'));
this.thumbItems[index].classList.add('active');
this.autoSlide();
}
next(){
let target = 0;
if(this.active < this.items.length - 1){
target = this.active + 1;
}
this.activeSlide(target);
}
prev(){
let target = this.items.length - 1;
if(this.active > 0){
target = this.active - 1;
}
this.activeSlide(target);
}
addNavigation(){
const nextBtn = this.slide.querySelector('.slide-next');
const prevBtn = this.slide.querySelector('.slide-prev');
nextBtn.addEventListener('click', this.next);
prevBtn.addEventListener('click', this.prev);
}
addThumbItems() {
this.items.forEach(() => {
this.thumb.innerHTML += `<span> </span>`;
});
this.thumbItems = Array.from(this.thumb.children);
console.log(this.thumbItems)
}
autoSlide(){
clearTimeout(this.timeout);
this.timeout = setTimeout(this.next, 3000);
}
init(){
this.items = this.slide.querySelectorAll('.slide-items > *');
this.thumb = this.slide.querySelector('.slide-thumb');
this.next = this.next.bind(this);
this.prev = this.prev.bind(this);
this.addThumbItems();
this.activeSlide(0);
this.addNavigation();
}
}
new SlideStories('slide');