Improve auto-scroll snapping

This commit is contained in:
Cohee 2024-07-22 23:07:51 +03:00
parent 256f0a58db
commit 12f6e5069d
1 changed files with 15 additions and 6 deletions

View File

@ -9253,12 +9253,21 @@ jQuery(async function () {
}
});
const chatElementScroll = document.getElementById('chat');
chatElementScroll.addEventListener('wheel', function () {
scrollLock = true;
}, { passive: true });
chatElementScroll.addEventListener('touchstart', function () {
scrollLock = true;
}, { passive: true });
const chatScrollHandler = function () {
const scrollIsAtBottom = Math.abs(chatElementScroll.scrollHeight - chatElementScroll.clientHeight - chatElementScroll.scrollTop) < 1;
// Cancel autoscroll if the user scrolls up
if (scrollLock && scrollIsAtBottom) {
scrollLock = false;
}
// Resume autoscroll if the user scrolls to the bottom
if (!scrollLock && !scrollIsAtBottom) {
scrollLock = true;
}
};
chatElementScroll.addEventListener('wheel', chatScrollHandler, { passive: true });
chatElementScroll.addEventListener('touchmove', chatScrollHandler, { passive: true });
chatElementScroll.addEventListener('scroll', function () {
if (is_use_scroll_holder) {
this.scrollTop = scroll_holder;