-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (96 loc) · 3.67 KB
/
index.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
const prevBtn = document.getElementById('carousel_prev');
const nextBtn = document.getElementById('carousel_next');
const carousel = document.getElementById('carousel');
const buttonPrev = document.getElementById('carousel_prev');
const buttonNext = document.getElementById('carousel_next');
const coruselImg = carousel.querySelectorAll('.carousel_item')[0];
const mobilePagination = document.getElementById('carousel_mobile_pagination');
let canDrag = false;
let prevPageX;
let prevScrollLeft;
let positionDiff;
let coruselImgWidth = coruselImg.clientWidth;
buttonPrev.addEventListener('click', () => {
carousel.scrollLeft -= coruselImgWidth
});
buttonNext.addEventListener('click', () => {
carousel.scrollLeft += coruselImgWidth
});
const startDrag = (e) => {
canDrag = true;
prevPageX = e.pageX || e.touches[0].pageX;
prevScrollLeft = carousel.scrollLeft;
carousel.classList.add('dragging');
}
const autoSlide = () => {
const allLinks = document.querySelectorAll('.carousel_mobile_pagination_link');
positionDiff = Math.abs(positionDiff);
let valDifference = coruselImgWidth - positionDiff
if (carousel.scrollLeft > prevScrollLeft) {
carousel.scrollLeft += positionDiff > coruselImgWidth / 6 ? valDifference : -positionDiff
} else {
carousel.scrollLeft -= positionDiff > coruselImgWidth / 6 ? valDifference : -positionDiff
}
allLinks.forEach((link, i) => {
if (Math.ceil(carousel.scrollLeft / coruselImgWidth) >= i + 1 || Math.ceil(carousel.scrollLeft / coruselImgWidth) <= i - 1) {
link.classList.remove('carousel_mobile_pagination_active_link');
} else {
link.classList.add('carousel_mobile_pagination_active_link');
}
})
}
const stopDrag = () => {
canDrag = false;
carousel.classList.remove('dragging');
autoSlide();
}
const dragging = (e) => {
if (!canDrag) { return };
e.preventDefault();
positionDiff = (e.pageX || e.touches[0].pageX) - prevPageX;
carousel.scrollLeft = prevScrollLeft - positionDiff;
}
carousel.addEventListener('mousedown', startDrag);
carousel.addEventListener('touchstart', startDrag);
carousel.addEventListener('mousemove', dragging);
carousel.addEventListener('touchmove', dragging);
carousel.addEventListener('mouseup', stopDrag);
carousel.addEventListener('touchend', stopDrag);
const getMobilePagination = () => {
const allSlides = carousel.querySelectorAll('.carousel_item')
allSlides.forEach((slide, index) => {
mobilePagination.innerHTML += `
<button
class="carousel_mobile_pagination_link"
onClick="navigateMobilePagination(${index})"
></button>
`
})
}
const navigateMobilePagination = (index) => {
const allLinks = document.querySelectorAll('.carousel_mobile_pagination_link');
carousel.scrollLeft = coruselImgWidth * index;
allLinks.forEach((link, i) => {
if (index === i) {
link.classList.add('carousel_mobile_pagination_active_link');
} else {
link.classList.remove('carousel_mobile_pagination_active_link');
}
})
}
getMobilePagination()
// button
const openMenuBtn = document.getElementById('header_open_menu_btn');
const closeMenuBtn = document.getElementById('mobile_navigation_button');
const mobileMenuy = document.getElementById('mobile_view_navigation');
const body = document.getElementsByTagName("BODY")[0];
function openMenu() {
mobileMenuy.style.display = 'grid';
body.style.overflow = 'hidden';
};
function closeMenu() {
mobileMenuy.style.display = 'none';
body.style.overflow = 'visible ';
};
openMenuBtn.addEventListener('click', openMenu);
closeMenuBtn.addEventListener('click', closeMenu);