-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (34 loc) · 1.14 KB
/
app.js
File metadata and controls
43 lines (34 loc) · 1.14 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
34
35
36
37
38
39
40
41
42
43
const sliderContainer = document.querySelector('.slider-container');
const slider = document.querySelector('.slider');
let clicked = false;
let xAxis;
let x;
sliderContainer.addEventListener('mouseup', () => {
sliderContainer.style.cursor = `grab`;
})
sliderContainer.addEventListener('mousedown', e => {
clicked = true
xAxis = e.offsetX - slider.offsetLeft;
// tells the current position
sliderContainer.style.cursor = `grabbing`
})
window.addEventListener('mouseup', () => {
clicked = false
})
sliderContainer.addEventListener('mousemove', e => {
if (!clicked) return;
e.preventDefault();
x = e.offsetX;
slider.style.left = `${x - xAxis}px`;
// but we dont want it to scroll forever
checkSize()
})
function checkSize () {
let sliderContainerOut = sliderContainer.getBoundingClientRect();
let sliderIn = slider.getBoundingClientRect();
if (parseInt(slider.style.left) > 0) {
slider.style.left = `0px`;
} else if (sliderIn.right < sliderContainerOut.right) {
slider.style.left = `-${sliderIn.width - sliderContainerOut.width}px`
}
}