-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (71 loc) · 3.12 KB
/
index.js
File metadata and controls
92 lines (71 loc) · 3.12 KB
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
const carousel = (function () {
function animateSlide() {
const slides = document.querySelectorAll('.Carousel__wrapper .Carousel__slide');
let isEnabled = true;
let currentSlide = 0;
function changeCurrentSlide(n) {
currentSlide = (n + slides.length) % slides.length;
}
function nextSlide(n) {
hideSlide('Carousel__to-left');
changeCurrentSlide(n + 1);
showSlide('Carousel__from-right');
}
function previousSlide(n) {
hideSlide('Carousel__to-right');
changeCurrentSlide(n - 1);
showSlide('Carousel__from-left');
}
function goToSlide(n) {
if (n < currentSlide) {
hideSlide('Carousel__to-right');
currentSlide = n;
showSlide('Carousel__from-left');
} else {
hideSlide('Carousel__to-left');
currentSlide = n;
showSlide('Carousel__from-right');
}
}
function hideSlide(direction) {
isEnabled = false;
slides[currentSlide].classList.add(direction);
slides[currentSlide].addEventListener('animationend', function() {
this.classList.remove('Carousel__active', direction);
});
}
function showSlide(direction) {
slides[currentSlide].classList.add('Carousel__next', direction);
slides[currentSlide].addEventListener('animationend', function() {
this.classList.remove('Carousel__next', direction);
this.classList.add('Carousel__active');
isEnabled = true;
});
}
document.querySelector('.Carousel__control.left').addEventListener('click', function() {
if (isEnabled) {
previousSlide(currentSlide);
}
});
document.querySelector('.Carousel__control.right').addEventListener('click', function() {
if (isEnabled) {
nextSlide(currentSlide);
}
});
}
function create(imageProps) {
const { anchorName, images } = imageProps
const anchor = document.querySelector(`[data-carousel-anchor=${anchorName}]`)
if (!anchor) {
throw new Error('Anchor name does not match anchor tag in html')
}
const image = images.reduce((accum, currentImage) =>
accum.concat(`<div class="Carousel__slide"><div class="Carousel__container"><img src="${currentImage}" /></div></div>`), '')
anchor.insertAdjacentHTML('beforeend', `<div class="Carousel__wrapper"><div class="Carousel__inner">${image}</div><div class="Carousel__control left"><div class="Carousel__arrow left"></div></div><div class="Carousel__control right"><div class="Carousel__arrow right"></div></div></div>` )
document.getElementsByClassName('Carousel__slide')[0].className = 'Carousel__slide Carousel__active'
animateSlide()
}
return {
create
}
})()