-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslider.js
More file actions
143 lines (117 loc) · 4.27 KB
/
slider.js
File metadata and controls
143 lines (117 loc) · 4.27 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
// extend a Siema class by two methods
// addDots - to create a markup for dots
// updateDots - to update classes on dots on change callback
delete window.SiemaWithDots
window.SiemaWithDots = class extends Siema {
addDots() {
// create a contnier for all dots
// add a class 'dots' for styling reason
this.dots = document.createElement("div");
this.dots.classList.add("dots");
// loop through slides to create a number of dots
for (let i = 0; i < this.innerElements.length; i++) {
// create a dot
const dot = document.createElement("button");
// add a class to dot
dot.classList.add("dots__item");
// add an event handler to each of them
dot.addEventListener("click", () => {
this.goTo(i);
});
// append dot to a container for all of them
this.dots.appendChild(dot);
}
// add the container full of dots after selector
this.selector.parentNode.insertBefore(this.dots, this.selector.nextSibling);
}
updateDots() {
// loop through all dots
for (let i = 0; i < this.dots.querySelectorAll("button").length; i++) {
// if current dot matches currentSlide prop, add a class to it, remove otherwise
const addOrRemove = this.currentSlide === i ? "add" : "remove";
const visited = this.currentSlide > i ? "add" : "remove";
this.dots
.querySelectorAll("button")
[i].classList[addOrRemove]("dots__item--active");
// Add middle circle to visited dots
this.dots
.querySelectorAll("button")
[i].classList[visited]("dots__item--visited");
}
}
updateControls() {
const isFirst = this.currentSlide === 0 ? "add" : "remove";
const isLast =
this.currentSlide === this.innerElements.length - 1 ? "add" : "remove";
document.querySelector(".prev").classList[isFirst]("controls--inactive");
document.querySelector(".next").classList[isLast]("controls--inactive");
}
}
// instantiate new extended Siema
delete window.mySiemaWithDots
window.mySiemaWithDots = new SiemaWithDots({
easing: "cubic-bezier(0.76, 0, 0.24, 1)",
duration: 500,
// on init trigger method created above
onInit: function () {
this.addDots();
this.updateDots();
},
onChange: function () {
this.updateDots();
this.updateControls();
},
});
// Controls
var prev = document.querySelector(".prev");
var next = document.querySelector(".next");
var dots = document.querySelector(".dots");
if (mySiemaWithDots.innerElements.length === 1) {
// Disable controls if only one slide
prev.parentNode?.removeChild(prev)
next.parentNode?.removeChild(next)
dots.parentNode?.removeChild(dots)
} else {
prev.addEventListener("click", () => mySiemaWithDots.prev());
next.addEventListener("click", () => mySiemaWithDots.next());
}
// Modal Handling
if (document.querySelector('.p_cards') !== null) {
for (let i = 0; i < document.querySelector('.p_cards').children.length; i++) {
const cards = [];
const modals = [];
cards[i] = document.querySelector(`#card-${i + 1}`);
modals[i] = document.querySelector(`#answer-${i + 1}`);
// cards[i] = document.querySelector('.p-card');
// modals[i] = document.querySelector('.modal-wrapper');
const modalWrappers = document.querySelectorAll(".modal-wrapper");
const closeBtns = document.querySelectorAll(".modal__btn");
const markers = document.querySelectorAll(".marker");
console.log(cards[i]);
if (cards && cards[i] && typeof cards[i] !== 'undefined') {
cards[i].addEventListener("click", () => {
modals[i].classList.toggle("is-active");
cards[i].children[2].style.opacity = 1;
});
}
for (const btn of closeBtns) {
btn.addEventListener("click", () => {
for (const wrapper of modalWrappers) {
wrapper.classList.remove("is-active");
}
for (const marker of markers) {
marker.style.opacity = 0;
}
});
}
}
}
// Disable Siema on small screens
if (screen.width <= 767) {
mySiemaWithDots.destroy();
document.querySelector('.siema > div').style.width = '100%';
for (let i = 0; i < mySiemaWithDots.innerElements.length; i++) {
mySiemaWithDots.innerElements[i].parentNode.style.float = 'initial';
mySiemaWithDots.innerElements[i].parentNode.style.width = '100%';
}
}