-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
94 lines (74 loc) · 2.46 KB
/
slider.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
import { createGrid, createHTMLElement, generateGrid } from "./helpers";
export class Slider {
constructor({ wrapSelector }) {
this.wrap = document.querySelector(wrapSelector);
this.gridSize = 3;
this.currentIndex = 0;
this.totalSlides = 3;
this.createSliderHTML();
this.listeners();
}
updateArrows() {
this.leftArrow.disabled = this.currentIndex === 0;
this.leftArrow.style.cursor =
this.currentIndex === 0 ? "default" : "pointer";
this.rightArrow.disabled = this.currentIndex === this.totalSlides - 1;
this.rightArrow.style.cursor =
this.currentIndex === this.totalSlides - 1 ? "default" : "pointer";
}
showSlide(index) {
this.slides.style.transform = `translateX(${-index * 100}%)`;
this.updateArrows();
}
createSliderHTML() {
const slider = createHTMLElement("div", { className: "slider" });
this.wrap.appendChild(slider);
this.slides = createHTMLElement("div", { className: "slides" });
slider.appendChild(this.slides);
this.leftArrow = createHTMLElement("button", {
className: "arrow left-arrow",
innerHTML: "◀",
});
this.rightArrow = createHTMLElement("button", {
className: "arrow right-arrow",
innerHTML: "▶",
});
this.wrap.append(this.leftArrow, this.rightArrow);
this.createGridSlidesHTML();
}
createGridSlidesHTML() {
for (let i = 0; i < this.totalSlides; i++) {
const slide = createHTMLElement("div", {
className: "slide",
});
this.slides.appendChild(slide);
this.showSlide(this.currentIndex);
const grid = createHTMLElement("div", {
className: "homepage-grid",
});
createGrid(generateGrid(i + this.gridSize), i + this.gridSize, grid);
const gridText = createHTMLElement("div", {
className: "homepage-grid-text",
textContent: `${i + this.gridSize} x ${i + this.gridSize}`,
});
slide.appendChild(grid);
grid.appendChild(gridText);
}
this.slideImages = document.querySelectorAll(".slide");
this.showSlide(this.currentIndex);
}
listeners() {
this.leftArrow.addEventListener("click", () => {
if (this.currentIndex > 0) {
this.currentIndex--;
this.showSlide(this.currentIndex);
}
});
this.rightArrow.addEventListener("click", () => {
if (this.currentIndex < this.totalSlides - 1) {
this.currentIndex++;
this.showSlide(this.currentIndex);
}
});
}
}