forked from Abhinav-ark/Amrita_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
87 lines (72 loc) · 3.01 KB
/
carousel.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
firstImg = carousel.querySelectorAll("img")[0],
arrowIcons = document.querySelectorAll(".wrapper i");
let isDragStart = false, isDragging = false, prevPageX, prevScrollLeft, positionDiff;
var iconref =0;
var scroll = [0,214,428];
const resetCarousel = () => {
iconref=0;
carousel.classList.add("dragging");
carousel.scrollLeft = 0;
carousel.classList.remove("dragging");
}
const showHideIcons = () => {
// showing and hiding prev/next icon according to carousel scroll left value
if(iconref>2){iconref=2}else{if(iconref<0){iconref=0}};
arrowIcons[0].style.display = iconref == 0 ? "none" : "block";
arrowIcons[1].style.display = iconref == 2 ? "none" : "block";
}
arrowIcons.forEach(icon => {
icon.addEventListener("click", () => {
let firstImgWidth = firstImg.clientWidth + 14; // getting first img width & adding 14 margin value
// if clicked icon is left, reduce width value from the carousel scroll left else add to it
carousel.scrollLeft += icon.id == "left" ? -firstImgWidth : firstImgWidth;
icon.id == "left" ? iconref = iconref-1 : iconref = iconref+1;
setTimeout(() => showHideIcons(), 60); // calling showHideIcons after 60ms
});
});
const autoSlide = () => {
// if there is no image left to scroll then return from here
positionDiff = Math.abs(positionDiff); // making positionDiff value to positive
let firstImgWidth = firstImg.clientWidth + 14;
// getting difference value that needs to add or reduce from carousel left to take middle img center
let valDifference = firstImgWidth - positionDiff;
console.log("kk");
if(carousel.scrollLeft > prevScrollLeft) { // if user is scrolling to the right
iconref=iconref+1;
showHideIcons();
return carousel.scrollLeft = scroll[iconref];
}
// if user is scrolling to the left
iconref = iconref-1;
showHideIcons();
carousel.scrollLeft =scroll[iconref];
}
const dragStart = (e) => {
// updatating global variables value on mouse down event
isDragStart = true;
prevPageX = e.pageX || e.touches[0].pageX;
prevScrollLeft = carousel.scrollLeft;
}
const dragging = (e) => {
// scrolling images/carousel to left according to mouse pointer
if(!isDragStart) return;
e.preventDefault();
isDragging = true;
carousel.classList.add("dragging");
positionDiff = (e.pageX || e.touches[0].pageX) - prevPageX;
carousel.scrollLeft = prevScrollLeft - positionDiff;
showHideIcons();
}
const dragStop = () => {
isDragStart = false;
carousel.classList.remove("dragging");
if(!isDragging) return;
isDragging = false;
autoSlide();
}
carousel.addEventListener("mousedown", dragStart);
carousel.addEventListener("touchstart", dragStart);
document.addEventListener("mousemove", dragging);
carousel.addEventListener("touchmove", dragging);
document.addEventListener("mouseup", dragStop);
carousel.addEventListener("touchend", dragStop);