-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragscroll.js
More file actions
33 lines (33 loc) · 1.03 KB
/
Copy pathdragscroll.js
File metadata and controls
33 lines (33 loc) · 1.03 KB
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
const dragscroll = document.getElementsByClassName('dragscroll');
let firstX = null,
scrollX = null,
firstY = null,
scrollY = null;
for (let i = 0; i < dragscroll.length; i++) {
const element = dragscroll[i];
element.addEventListener('mousemove', e => {
if (e.which === 1) {
if (firstX === null) {
//* Set initial values ---
firstX = e.layerX;
scrollX = element.scrollLeft;
firstY = e.layerY;
scrollY = element.scrollTop;
}
element.scrollTo({
// * Calculate and adjust the scroll value ---
top: scrollY + (firstY - e.layerY),
left: scrollX + (firstX - e.layerX),
behavior: "auto"
});
}
else {
//* Reset the initial values ---
firstX = null;
scrollX = null;
firstY = null;
scrollY = null;
}
});
}
//? Author : https://github.com/MohammadAliHeidary