-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (142 loc) · 3.82 KB
/
Copy pathscript.js
File metadata and controls
172 lines (142 loc) · 3.82 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class HDCarousel {
version = 0.1;
el = null; // element housing the carousel
items = []; // array of carousel items
size = 3; // total items to display
activeClass = false; // if middle item should get an active class
gap = 22; // margin/gap in px
width = 0;
constructor(el, settings = {}) {
console.log("HDCarousel v" + this.version + " init");
if (settings !== {}) {
if (settings.gap) {
this.gap = parseInt(settings.gap);
}
if (settings.size) {
this.size = parseInt(settings.size);
}
if (settings.activeClass) {
this.activeClass = true;
}
}
this.el = el;
this.init();
}
async init() {
await this.createMarkup();
// set nav listeners
const nav = this.el.parentElement.getElementsByClassName("hdcarousel_nav_item");
for (let i = 0; i < nav.length; i++) {
nav[i].addEventListener("click", () => this.move(nav[i]));
}
await this.setMinItems();
this.width = await this.getSize();
this.el.style.height = await this.getHeight();
await this.clone("prev");
await this.build();
}
async createMarkup() {
const nav = `<div class="hdcarousel_nav"><div class="hdcarousel_nav_item" aria-role="button" data-dir="prev">«</div><div class="hdcarousel_nav_item" aria-role="button" data-dir="next">»</div></div>`;
// create new element
let wrapper = document.createElement("div");
wrapper.classList.add("hdcarousel_wrapper");
// clone carousel and insert into the wrapper
let carousel = this.el.cloneNode(true);
wrapper.insertAdjacentElement("afterbegin", carousel);
wrapper.insertAdjacentHTML("beforeend", nav);
// add the new wrapper before the old carousel
this.el.insertAdjacentElement("beforebegin", wrapper);
// remove the old carousel and reset the variables
this.el.remove();
this.el = wrapper.firstChild;
this.el.classList.add("hdcarousel");
console.log(this.el);
this.items = this.el.getElementsByClassName("hdcarousel_item");
}
async setMinItems() {
const minItems = this.size + 2;
if (this.items.length < minItems) {
let itemsLength = this.items.length;
for (let i = 0; i < itemsLength; i++) {
let c = this.items[i].cloneNode(true);
this.el.append(c);
}
}
if (this.items.length < minItems) {
await this.setMinItems();
}
}
async getSize() {
let w = this.el.clientWidth;
w = w / this.size - this.gap;
return w;
}
async getHeight() {
let h = this.items[0].clientHeight;
// check if another item is higher
for (let i = 0; i < this.items.length; i++) {
let item_h = this.items[i].clientHeight;
if (item_h > h) {
h = item_h;
}
}
return h + "px";
}
async build() {
let l = this.width * -1;
for (let i = 0; i < this.items.length; i++) {
this.items[i].style.width = this.width + "px";
this.items[i].style.left = l + "px";
l = l + this.width;
if (i > 0) {
let g = this.gap / this.size;
l = l + this.gap + g;
}
}
if (this.activeClass) {
this.setActive();
}
}
async clone(pos = "next") {
let item = null;
if (pos === "next") {
item = this.items[0];
} else {
item = this.items[this.items.length - 1];
}
let c = item.cloneNode(true);
if (pos === "next") {
this.el.append(c);
} else {
this.el.prepend(c);
}
item.remove();
}
async move(el) {
let pos = el.getAttribute("data-dir");
if (pos === "next") {
this.next();
} else {
this.prev();
}
}
async next() {
await this.clone("next");
await this.build();
}
async prev() {
await this.clone("prev");
await this.build();
}
setActive() {
let m = Math.round(this.size / 2);
for (let i = 0; i < this.items.length; i++) {
this.items[i].classList.remove("hdcarousel_item_active");
if (i === m) {
this.items[i].classList.add("hdcarousel_item_active");
}
}
}
}
const el = document.getElementById("like_and_subscribe");
new HDCarousel(el, { activeClass: true });