-
Notifications
You must be signed in to change notification settings - Fork 0
/
testimonials-slider.js
52 lines (45 loc) · 1.5 KB
/
testimonials-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
document.addEventListener('DOMContentLoaded', function() {
const slider = document.querySelector('.testimonial-slider');
let isDown = false;
let startX;
let scrollLeft;
slider.addEventListener('mousedown', (e) => {
isDown = true;
slider.classList.add('active');
startX = e.pageX - slider.offsetLeft;
scrollLeft = slider.scrollLeft;
});
slider.addEventListener('mouseleave', () => {
isDown = false;
slider.classList.remove('active');
});
slider.addEventListener('mouseup', () => {
isDown = false;
slider.classList.remove('active');
});
slider.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - slider.offsetLeft;
const walk = (x - startX) * 2;
slider.scrollLeft = scrollLeft - walk;
});
// Touch events for mobile devices
slider.addEventListener('touchstart', (e) => {
isDown = true;
slider.classList.add('active');
startX = e.touches[0].pageX - slider.offsetLeft;
scrollLeft = slider.scrollLeft;
});
slider.addEventListener('touchend', () => {
isDown = false;
slider.classList.remove('active');
});
slider.addEventListener('touchmove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.touches[0].pageX - slider.offsetLeft;
const walk = (x - startX) * 2;
slider.scrollLeft = scrollLeft - walk;
});
});