-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
29 lines (29 loc) · 1.35 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
function sliderActived(){
window.addEventListener(`load`, function(){
const sliderELe = document.querySelectorAll('[data-tg="sliderBox"]');
sliderELe.forEach((element) => {
const container = element.querySelector(`[data-tg="slideable"]`);
let img = element.querySelector(`img`);
element.style.width = img.clientWidth + "px";
element.style.height = img.clientHeight + "px";
const slides = container.children;
const prevButton = element.querySelector('[data-tg="prev"]');
const nextButton = element.querySelector('[data-tg="next"]');
let currentSlideIndex = 0;
slides[currentSlideIndex].setAttribute("data-tg", "action");
nextButton.addEventListener("click", () => {
slides[currentSlideIndex].removeAttribute("data-tg");
currentSlideIndex = (currentSlideIndex + 1) % slides.length;
slides[currentSlideIndex].setAttribute("data-tg", "action");
container.style.transform = `translateX(-${100 * currentSlideIndex}%)`;
});
prevButton.addEventListener("click", () => {
slides[currentSlideIndex].removeAttribute("data-tg");
currentSlideIndex = (currentSlideIndex - 1 + slides.length) % slides.length;
slides[currentSlideIndex].setAttribute("data-tg", "action");
container.style.transform = `translateX(-${100 * currentSlideIndex}%)`;
});
});
});
}
export{sliderActived}