-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·180 lines (169 loc) · 5.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Author : Kean Duque
* Project : Slider Component
*/
import data from "./data.json" assert { type: "json" };
("use strict");
class Slider {
scrollbarInner = document.querySelector(".scroll-inner-bar");
sliderNavLeft = document.querySelectorAll(".slider__btn--left");
sliderNavRight = document.querySelectorAll(".slider__btn--right");
//initialize slider
constructor(sliderWrapper, options) {
const slider = document.querySelector(sliderWrapper);
//render Cards
this.card(slider, options);
//render Events
this.events(slider, options);
}
events(slider, options) {
const self = this;
const getWindowWidth = window.innerWidth;
const touchOptions = {
imgClass: options.cardImgClass,
slider: slider,
cards: options.cards,
};
this.sliderTouchSlide(touchOptions);
// navigation left right button control
if (options.navControl === false) {
document.querySelector(".controls").style.visibility = "hidden";
} else {
this.sliderNav(options);
}
// check size of data and scrensize upon load and resize
window.addEventListener("load", function () {
self.scrollBarDisplay(getWindowWidth, options);
});
window.addEventListener("resize", function () {
self.scrollBarDisplay(getWindowWidth, options);
});
}
// check if the size of window match the criteria to show scroll or data is < 4 || 2
scrollBarDisplay(windowWidth, options) {
const scrollContainer = document.querySelector(".scroll-container");
if (windowWidth > 768 && options.cards.length < 4) {
scrollContainer.style.visibility = "hidden";
} else if (windowWidth < 768 && options.cards.length <= 2) {
scrollContainer.style.visibility = "hidden";
} else {
scrollContainer.style.visibility = "visible";
}
}
// control for touching slider
sliderTouchSlide(touchOptions) {
const self = this;
const scrollBar = self.scrollbarInner;
const slider = touchOptions.slider;
const cardImgClass = touchOptions.imgClass;
const imgDrag = document.querySelector(`.${cardImgClass}`);
let sliderGrabbed = false;
slider.parentElement.addEventListener("scroll", (e) => {
scrollBar.style.width = `${this.getScrollPercentage(slider)}%`;
});
slider.addEventListener("mousedown", (e) => {
sliderGrabbed = true;
slider.style.cursor = "grabbing";
});
slider.addEventListener("mouseup", (e) => {
sliderGrabbed = false;
slider.style.cursor = "grab";
});
slider.addEventListener("mouseleave", (e) => {
sliderGrabbed = false;
});
slider.addEventListener("mousemove", (e) => {
if (sliderGrabbed) slider.parentElement.scrollLeft -= e.movementX;
});
slider.addEventListener("wheel", (e) => {
e.preventDefault();
slider.parentElement.scrollLeft += e.deltaY;
});
if (imgDrag)
imgDrag.addEventListener("mousedown", (e) => {
return false;
});
}
// controls for desktop navigation left and right
sliderNav(options) {
const self = this;
const sliderContent = document.querySelectorAll(
`.${options.sliderContentClass}`
);
const sliderLeft = self.sliderNavLeft;
const sliderRight = self.sliderNavRight;
const sliderContainers = [...sliderContent];
const btnLeft = [...sliderLeft];
const btnRight = [...sliderRight];
sliderContainers.forEach((item, i) => {
let dimension = item.getBoundingClientRect();
let containerWidth = dimension.width;
btnRight[i].addEventListener("click", () => {
item.scrollLeft += containerWidth;
});
btnLeft[i].addEventListener("click", () => {
item.scrollLeft -= containerWidth;
});
});
}
// create cards
card(sliderWrapper, options) {
const cards = [...options.cards];
const path = options.path;
const draggable = options.draggable;
const cardClass = options.cardClass;
const cardImgClass = options.cardImgClass;
const getClass = sliderWrapper.getAttribute("class");
let render = "";
if (cards.length > 0) {
cards.forEach((item, i) => {
render += `<div class="${cardClass}">
<div class="card-img-content">
<img
class="${cardImgClass}"
src="${path + item.imgSrc}"
alt="${item.name}"
draggable=${draggable} />
</div>
<div class="card-content">
<h2 class="card-content-name">${item.name}</h2>
<p class="card-content-price">${item.price}</p>
<button class="card-content-button">${options.btnTitle}</button>
</div>
</div>`;
});
} else {
const wrapperDiv = document.getElementsByClassName(getClass)[0];
wrapperDiv.style.position = "initial";
wrapperDiv.style.backgroundColor = "#f9f9f9";
wrapperDiv.style.borderRadius = "10px";
render += `<div class="${cardClass} no-data">
<h2 class="card-content-name">No Data</h2>
</div>`;
}
const divSlider = sliderWrapper;
divSlider.innerHTML += render;
return render;
}
// get the scroll percentage for slide touch
getScrollPercentage(slider) {
return (
(slider.parentElement.scrollLeft /
(slider.parentElement.scrollWidth -
slider.parentElement.clientWidth)) *
100
);
}
}
//change settings here for the slider
const config = {
cardClass: "card",
cardImgClass: "card-img",
path: "public/img/",
draggable: false,
cards: data,
btnTitle: "Add to Cart",
navControl: false,
sliderContentClass: "slider-content",
};
const initialize = new Slider(".slider-wrapper", config);