-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
86 lines (69 loc) · 2.58 KB
/
index.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
84
85
86
const DEFAULT_OPTIONS = {perPage: 3, gap: 16};
class Slider{
constructor(selector, options = DEFAULT_OPTIONS) {
this.selector = selector;
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
this.items = null;
this.contentWidth = 0;
this.maxScroll = 0;
window.addEventListener("load", () => this.mount());
window.addEventListener("resize", () => this.mount());
}
isValid(){
return Boolean(this.items) && this.contentWidth > 0 && this.maxScroll > 0;
}
mount(){
const slider = document.querySelector(this.selector);
if(!slider){
console.error(`${this.selector} selector not found!`);
return;
}
const content = slider.querySelector('.slider-content');
if(!content){
console.error(`'slider-content' class not found in ${this.selector} selector.`)
return;
}
this.contentWidth = content.offsetWidth;
this.items = slider.querySelector('.slider-items');
if(!this.items){
console.error(`'slider-items' class not found in ${this.selector} selector.`)
return;
}
const itemCount = this.items.children.length;
const {gap, perPage} = this.options;
const contentWidthWithoutGap = this.contentWidth - (gap * perPage);
const perItemWidth = Math.floor(contentWidthWithoutGap / perPage);
this.items.style.width = (itemCount * (perItemWidth + gap)) + 'px';
for (const item of this.items.children) {
item.style.margin = gap + 'px';
item.style.width = perItemWidth + 'px';
}
this.maxScroll = Math.floor(itemCount / perPage) * this.contentWidth;
this.scroll = 0;
// NAVIGATION INITIALIZE
const [navigationPrev, navigationNext] = slider.getElementsByClassName('slider-navigation');
if(navigationPrev) navigationPrev.onclick = () => this.prev();
if(navigationNext) navigationNext.onclick = () => this.next();
}
prev(){
if(!this.isValid()){
return;
}
this.scroll += this.contentWidth;
if(this.scroll > 0){
this.scroll = -this.maxScroll;
}
this.items.style.transform = `translateX(${this.scroll}px)`;
}
next(){
if(!this.isValid()){
return;
}
this.scroll -= this.contentWidth;
if(this.scroll < (-this.maxScroll)){
this.scroll = 0;
}
this.items.style.transform = `translateX(${this.scroll}px)`;
}
}
module.exports = Slider;