-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
151 lines (132 loc) · 4.2 KB
/
script.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const HDS = function (el = null, options = {}) {
class slider {
constructor(options) {
this.delay = 0; // delay in ms to begin the timer
this.speed = 5000; // time in ms 'till next slide
this.transition = 600; // time in ms for animation speed
this.pagination = true;
this.navigation = true;
for (const k in options) {
this[k] = options[k];
}
this.delay = parseInt(this.delay);
this.speed = parseInt(this.speed);
this.transition = parseInt(this.transition);
}
}
const $ = {
VARS: {
slider: null,
slides: null,
options: {},
timer: null,
pagination: null,
},
init: async function () {
if (el === null) {
console.error("Please select a container for the slider");
return;
}
$.VARS.slider = document.querySelector(el);
$.VARS.options = new slider(options);
await $.create();
},
create: async function () {
const slides = document.createElement("div");
slides.classList.add("hds_slides_container");
const content = $.VARS.slider.innerHTML;
slides.innerHTML = content;
$.VARS.slider.innerHTML = "";
$.VARS.slider.append(slides);
$.VARS.slider.classList.add("hds_slider");
$.VARS.slides = $.VARS.slider.getElementsByClassName("hds_slides_container")[0].children;
for (let i = 0; i < $.VARS.slides.length; i++) {
$.VARS.slides[i].classList.add("hds_slide");
}
$.VARS.slider.style.setProperty("--slide-transition-speed", $.VARS.options.transition + "ms");
if ($.VARS.options.navigation === true) {
$.navigation();
}
if ($.VARS.options.pagination === true) {
$.pagination();
}
$.setActive($.VARS.slides[0]);
setTimeout($.start, $.VARS.options.delay);
},
setActive: function (slide) {
for (let i = 0; i < $.VARS.slides.length; i++) {
$.VARS.slides[i].classList.remove("hds_slide_active");
}
slide.classList.add("hds_slide_active");
if ($.VARS.options.pagination === true) {
let index = 0;
for (let i = 0; i < $.VARS.slides.length; i++) {
if ($.VARS.slides[i].classList.contains("hds_slide_active")) {
index = i;
break;
}
}
for (let i = 0; i < $.VARS.pagination.length; i++) {
$.VARS.pagination[i].classList.remove("hds_pagination_item_active");
}
$.VARS.pagination[index].classList.add("hds_pagination_item_active");
}
},
start: function () {
$.VARS.timer = setInterval($.next, $.VARS.options.speed);
},
next: function () {
clearInterval($.VARS.timer);
const active = $.VARS.slider.getElementsByClassName("hds_slide_active")[0];
if (active.nextElementSibling === null) {
$.setActive($.VARS.slides[0]);
} else {
$.setActive(active.nextElementSibling);
}
$.start();
},
prev: function () {
clearInterval($.VARS.timer);
const active = $.VARS.slider.getElementsByClassName("hds_slide_active")[0];
if (active.previousElementSibling === null) {
$.setActive($.VARS.slides[$.VARS.slides.length - 1]);
} else {
$.setActive(active.previousElementSibling);
}
$.start();
},
navigation: function () {
const nav = `<div class = "hds_nav_item hds_prev"><</div><div class = "hds_nav_item hds_next">></div>`;
$.VARS.slider.insertAdjacentHTML("beforeend", nav);
const prev = $.VARS.slider.getElementsByClassName("hds_prev")[0];
const next = $.VARS.slider.getElementsByClassName("hds_next")[0];
prev.addEventListener("click", $.prev);
next.addEventListener("click", $.next);
},
pagination: function () {
let html = '<div class = "hds_pagination">';
for (let i = 0; i < $.VARS.slides.length; i++) {
html += `<div class = "hds_pagination_item"></div>`;
}
html += "</div>";
$.VARS.slider.insertAdjacentHTML("beforeend", html);
$.VARS.pagination = $.VARS.slider.getElementsByClassName("hds_pagination_item");
for (let i = 0; i < $.VARS.pagination.length; i++) {
$.VARS.pagination[i].addEventListener("click", function () {
if (this.classList.contains("hds_pagination_item_active")) {
return;
}
let index = 0;
for (let i = 0; i < $.VARS.pagination.length; i++) {
if ($.VARS.pagination[i] === this) {
index = i;
break;
}
}
$.setActive($.VARS.slides[index]);
});
}
},
};
$.init(el, options);
};