Skip to content

Commit 4ecace2

Browse files
committed
Improve code explinations
1 parent 18d5182 commit 4ecace2

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/smoothscroll.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,18 @@ function polyfill() {
222222
method = scrollElement;
223223
}
224224

225-
const maxDistance = Math.max(Math.abs(x - startX), Math.abs(y - startY));
226-
const scrollTime = Math.max(
225+
// Calculate the maximum distance to scroll in pixels
226+
const distanceToScrollX = Math.abs(x - startX);
227+
const distanceToScrollY = Math.abs(y - startY);
228+
const maxDistanceToScroll = Math.max(distanceToScrollX, distanceToScrollY);
229+
// Configure the speed animation in pixels/ms
230+
const animationSpeed = 1;
231+
// Calculate the time needed for the scroll animation
232+
const scrollTime =
233+
Math.max(
227234
MIN_SCROLL_TIME,
228-
Math.min(MAX_SCROLL_TIME, maxDistance)
229-
);
235+
Math.min(MAX_SCROLL_TIME, maxDistanceToScroll)
236+
) * animationSpeed;
230237

231238
// scroll looping over a frame
232239
step({
@@ -262,8 +269,8 @@ function polyfill() {
262269
arguments[0].top !== undefined
263270
? arguments[0].top
264271
: arguments[1] !== undefined
265-
? arguments[1]
266-
: w.scrollY || w.pageYOffset
272+
? arguments[1]
273+
: w.scrollY || w.pageYOffset
267274
);
268275

269276
return;

0 commit comments

Comments
 (0)