Skip to content

Commit 6d8e7e2

Browse files
committed
Improve code explinations
1 parent 18d5182 commit 6d8e7e2

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/smoothscroll.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,17 @@ 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)
235+
Math.min(MAX_SCROLL_TIME, maxDistanceToScroll * animationSpeed)
229236
);
230237

231238
// scroll looping over a frame
@@ -256,14 +263,14 @@ function polyfill() {
256263
arguments[0].left !== undefined
257264
? arguments[0].left
258265
: typeof arguments[0] !== 'object'
259-
? arguments[0]
260-
: w.scrollX || w.pageXOffset,
266+
? arguments[0]
267+
: w.scrollX || w.pageXOffset,
261268
// use top prop, second argument if present or fallback to scrollY
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)