Properly calculate auto scroll px amount
"scrollBy" and "scrollTo" require a change of at least 1px, otherwise, nothing will happen
This commit is contained in:
@@ -10,23 +10,19 @@ import { MutableRefObject, useCallback, useEffect, useMemo, useRef, useState } f
|
|||||||
import { ScrollDirection } from '@/modules/core/Core.types.ts';
|
import { ScrollDirection } from '@/modules/core/Core.types.ts';
|
||||||
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
|
import { useResizeObserver } from '@/modules/core/hooks/useResizeObserver.tsx';
|
||||||
|
|
||||||
|
// "scrollBy" and "scrollTo" both require at least a change of 1px, otherwise, nothing happens
|
||||||
const MIN_SCROLL_AMOUNT_PX = 1.5;
|
const MIN_SCROLL_AMOUNT_PX = 1.5;
|
||||||
const getScrollAmount = (
|
const getScrollAmount = (
|
||||||
amountPerMs: number,
|
amountPerMs: number,
|
||||||
speedMs: number,
|
speedMs: number,
|
||||||
isRTL: boolean = false,
|
isRTL: boolean = false,
|
||||||
invert: boolean = false,
|
invert: boolean = false,
|
||||||
): { amountPx: number; speedMs: number } => {
|
): number => {
|
||||||
const amountPx = amountPerMs * speedMs;
|
const pxPerMs = Math.max(amountPerMs * speedMs, MIN_SCROLL_AMOUNT_PX);
|
||||||
|
const pxPerMsReadingMode = isRTL ? -pxPerMs : pxPerMs;
|
||||||
if (amountPx >= MIN_SCROLL_AMOUNT_PX) {
|
|
||||||
const pxPerMsReadingMode = isRTL ? -amountPx : amountPx;
|
|
||||||
const pxPerMsInverted = invert ? pxPerMsReadingMode * -1 : pxPerMsReadingMode;
|
const pxPerMsInverted = invert ? pxPerMsReadingMode * -1 : pxPerMsReadingMode;
|
||||||
|
|
||||||
return { amountPx: pxPerMsInverted, speedMs };
|
return pxPerMsInverted;
|
||||||
}
|
|
||||||
|
|
||||||
return getScrollAmount(amountPerMs, speedMs + 1);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getPxPerMs = (size: number, scrollAmountPercentage: number, scrollSpeedMs: number): number =>
|
const getPxPerMs = (size: number, scrollAmountPercentage: number, scrollSpeedMs: number): number =>
|
||||||
|
|||||||
Reference in New Issue
Block a user