-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.js
83 lines (63 loc) · 1.95 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
77
78
79
80
81
82
83
let carousel = getCarouselAndClasses().carousel;
if (carousel) {
let
ctrlLeft = carousel.querySelector('.carousel-control--left'),
ctrlRight = carousel.querySelector('.carousel-control--right');
setCarouselHeight();
ctrlLeft.onclick = function(e) {
e.preventDefault();
let
items = getCarouselItems(),
classes = getCarouselAndClasses();
items.left.classList.add(classes.right);
items.left.classList.remove(classes.left);
items.right.classList.add(classes.middle);
items.right.classList.remove(classes.right);
items.middle.classList.add(classes.left);
items.middle.classList.remove(classes.middle);
};
ctrlRight.onclick = function(e) {
e.preventDefault();
let
items = getCarouselItems(),
classes = getCarouselAndClasses();
items.right.classList.add(classes.left);
items.right.classList.remove(classes.right);
items.left.classList.add(classes.middle);
items.left.classList.remove(classes.left);
items.middle.classList.add(classes.right);
items.middle.classList.remove(classes.middle);
};
}
function getCarouselAndClasses() {
return {
left: 'carousel-item--left',
middle: 'carousel-item--middle',
right: 'carousel-item--right',
carousel: document.querySelector('.carousel'),
}
}
function getCarouselItems() {
let classes = getCarouselAndClasses();
return {
left: carousel.querySelector('.' + classes.left),
middle: carousel.querySelector('.' + classes.middle),
right: carousel.querySelector('.' + classes.right),
}
}
function setCarouselHeight() {
let
cAndC = getCarouselAndClasses(),
carousel = cAndC.carousel,
carouselHeight = carousel.querySelector('.' + cAndC.middle).offsetHeight,
height = carouselHeight + 'px';
carousel.style.height = height;
carousel.querySelector('.carousel__items').style.height = height;
}
let carouselResizeTimeout;
window.onresize = function(e) {
clearTimeout(carouselResizeTimeout);
carouselResizeTimeout = setTimeout(function(){
setCarouselHeight();
}, 250);
};