From b9fbb9c3cc1955e314407dc567ea1ec9b0c86e1e Mon Sep 17 00:00:00 2001 From: antoine92190 Date: Mon, 22 Dec 2025 17:06:34 +0100 Subject: [PATCH] [bug fix] improve scrolling speed when dragging --- src/utils.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 171f09ea0..d81a72e35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -590,28 +590,31 @@ export class Utils { const elRect = el.getBoundingClientRect(); const scrollRect = scrollEl.getBoundingClientRect(); const innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight); + const prevScroll = scrollEl.scrollTop; + // Calculate how far element extends past viewport edges const offsetDiffDown = elRect.bottom - Math.min(scrollRect.bottom, innerHeightOrClientHeight); const offsetDiffUp = elRect.top - Math.max(scrollRect.top, 0); - const prevScroll = scrollEl.scrollTop; + + // Cap the maximum scroll speed per update + const maxScrollPerUpdate = 10; + let scrollAmount = 0; if (offsetDiffUp < 0 && distance < 0) { - // scroll up - if (el.offsetHeight > scrollRect.height) { - scrollEl.scrollTop += distance; - } else { - scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp; - } + // Element is above viewport, scroll up + // Limit scroll speed to maxScrollPerUpdate + scrollAmount = Math.max(offsetDiffUp, -maxScrollPerUpdate); } else if (offsetDiffDown > 0 && distance > 0) { - // scroll down - if (el.offsetHeight > scrollRect.height) { - scrollEl.scrollTop += distance; - } else { - scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown; - } + // Element is below viewport, scroll down + // Limit scroll speed to maxScrollPerUpdate + scrollAmount = Math.min(offsetDiffDown, maxScrollPerUpdate); } - position.top += scrollEl.scrollTop - prevScroll; + // Apply scroll + if (scrollAmount !== 0) { + scrollEl.scrollTop += scrollAmount; + position.top += scrollEl.scrollTop - prevScroll; + } } /**