-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliderSmoothScroll.js
69 lines (61 loc) · 1.94 KB
/
sliderSmoothScroll.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
let sliderScroll = null;
let intervalScrooling = null;
let scrollIncrement = 50;
let isToScroll = false;
let sliderWidthLimitMin = 0;
let sliderWidthLimitMax = 0;
const hoverStyleLeft = "slider-scroll-hover-left";
const hoverStyleRight = "slider-scroll-hover-right";
window.addEventListener("load", () => {
sliderScroll = document.querySelector("#slider-scroll");
getSliderLimits();
});
window.addEventListener("resize", () => {
getSliderLimits();
});
function verifyIfIsMobile() {
return (window.innerWidth < 760);
}
function getSliderLimits(){
const sliderWidth = sliderScroll.offsetWidth;
sliderWidthLimitMin = sliderWidth * 5 / 100;
sliderWidthLimitMax = sliderWidth * 95 / 100;
}
function handleMouseLeaveSlider(event) {
window.clearInterval(intervalScrooling);
intervalScrooling = null;
removeSliderHoverStyle();
}
function smoothScroll(){
if(isToScroll){
sliderScroll.scrollBy(scrollIncrement, 0);
}
}
function removeSliderHoverStyle(){
sliderScroll.classList.remove(hoverStyleLeft, hoverStyleRight);
}
function handleMouseMoveSlider(event){
if (!event || verifyIfIsMobile()){
return;
}
const mouseX = event.clientX;
if (intervalScrooling == null){
intervalScrooling = window.setInterval(() => { smoothScroll(); }, 100);
}
if (mouseX <= sliderWidthLimitMin && !(mouseX > sliderWidthLimitMin)){
/* LEFT SIDE */
isToScroll = true;
sliderScroll.classList.add(hoverStyleLeft);
scrollIncrement = -Math.abs(scrollIncrement);
}else if(mouseX >= sliderWidthLimitMax && !(mouseX < sliderWidthLimitMax)){
/* RIGHT SIDE */
isToScroll = true;
sliderScroll.classList.add(hoverStyleRight);
scrollIncrement = Math.abs(scrollIncrement);
}else{
isToScroll = false;
window.clearInterval(intervalScrooling);
removeSliderHoverStyle();
intervalScrooling = null;
}
}