-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
79 lines (66 loc) · 2.83 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
class Carousel {
constructor(carousel) {
this.carousel = carousel;
this.leftButton = this.carousel.querySelector('.left-button');
this.rightButton = this.carousel.querySelector('.right-button');
this.images = this.carousel.querySelectorAll('img');
this.leftButton.addEventListener('click', () => this.leftButtonPress());
this.rightButton.addEventListener('click', () => this.rightButtonPress());
}
rightButtonPress() {
const images = this.images;
const length = images.length;
for (let i=0; i<length; i++) {
if (images[length-1].classList.contains('active-img') && images[0].classList.contains('active-img')) {
images[length-1].classList.remove('active-img');
images[1].classList.add('active-img');
images[0].style.order = 0;
break;
}
if (images[length-1].classList.contains('active-img') && images[length-2].classList.contains('active-img')) {
images[length-2].classList.remove('active-img');
images[0].classList.add('active-img');
images[length-1].style.order = -1;
break;
}
if (images[1].classList.contains('active-img')) {
images[length-1].style.order = 0;
}
if (images[i].classList.contains('active-img')) {
images[i].classList.remove('active-img');
images[i+2].classList.add('active-img');
break;
}
}
images.forEach( img => img.classList.contains('active-img') ? TweenMax.from(img, .8, {x: img.width, ease:Power3.easeOut}) : img.style.x !==0 ? TweenMax.to(img, .01, {x:0}) : null );
}
leftButtonPress() {
const images = this.images;
const length = images.length;
for (let i=length-1; i>=0; i--) {
if (images[0].classList.contains('active-img') && images[1].classList.contains('active-img')) {
images[1].classList.remove('active-img');
images[0].style.order = 1;
images[length-1].classList.add('active-img');
break;
}
if (images[1].classList.contains('active-img') && images[2].classList.contains('active-img')) {
images[0].style.order = 0;
}
if (images[length-1].classList.contains('active-img') && images[0].classList.contains('active-img')) {
images[0].classList.remove('active-img');
images[length-1].style.order = 0;
images[length-2].classList.add('active-img');
break;
}
if (images[i].classList.contains('active-img')) {
images[i].classList.remove('active-img');
images[i-2].classList.add('active-img');
break;
}
}
images.forEach( img => img.classList.contains('active-img') ? TweenMax.from(img, .8, {x: -img.width, ease:Power3.easeOut}) : img.style.x !== 0 ? TweenMax.to(img, .01, {x:0}) : null );
}
}
const carouselSelect = document.querySelector('.carousel');
const carousel = new Carousel(carouselSelect);