Skip to content
Snippets Groups Projects
Commit 8e51d539 authored by akiraohgaki's avatar akiraohgaki
Browse files

Fix smooth scrolling

parent 71cfa071
No related branches found
No related tags found
No related merge requests found
const {ipcRenderer} = require('electron');
function applySmoothScroll() {
let timeoutId = null;
let requestId = null;
let frame = 0;
const frameMax = 50;
let amount = 0;
const amountMax = 7;
let deltaX = 0;
let deltaY = 0;
const amount = 2;
let scrollLeft = 0;
let scrollTop = 0;
document.scrollingElement.addEventListener('wheel', (event) => {
event.preventDefault();
const easeOut = (p) => {
return p * (2 - p);
};
deltaX += event.deltaX * amount;
deltaY += event.deltaY * amount;
const scroll = () => {
frame++;
document.scrollingElement.scrollTo({
left: scrollLeft + (deltaX * easeOut(frame / frameMax)),
top: scrollTop + (deltaY * easeOut(frame / frameMax))
});
if (frame === frameMax) {
frame = 0;
amount = 0;
cancelAnimationFrame(requestId);
}
else {
requestId = requestAnimationFrame(scroll);
}
};
if (timeoutId === null) {
timeoutId = setTimeout(() => {
document.scrollingElement.scrollBy({
left: deltaX,
top: deltaY,
behavior: 'smooth'
});
timeoutId = null;
deltaX = 0;
deltaY = 0;
}, 150);
document.scrollingElement.addEventListener('wheel', (event) => {
event.preventDefault();
if (Math.sign(deltaX) !== Math.sign(event.deltaX)
|| Math.sign(deltaY) !== Math.sign(event.deltaY)
) {
amount = 0;
}
if (amount < amountMax) {
amount++;
}
deltaX = event.deltaX * amount;
deltaY = event.deltaY * amount;
scrollLeft = document.scrollingElement.scrollLeft;
scrollTop = document.scrollingElement.scrollTop;
if (frame) {
frame = 0;
cancelAnimationFrame(requestId);
}
scroll();
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment