Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/smoothscroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function polyfill() {

// globals
var Element = w.HTMLElement || w.Element;
var SCROLL_TIME = 468;
var MIN_SCROLL_TIME = 200;
var MAX_SCROLL_TIME = 468;

// object gathering original scroll methods
var original = {
Expand Down Expand Up @@ -174,7 +175,7 @@ function polyfill() {
var value;
var currentX;
var currentY;
var elapsed = (time - context.startTime) / SCROLL_TIME;
var elapsed = (time - context.startTime) / context.scrollTime;

// avoid elapsed times higher than one
elapsed = elapsed > 1 ? 1 : elapsed;
Expand Down Expand Up @@ -221,6 +222,12 @@ function polyfill() {
method = scrollElement;
}

const maxDistance = Math.max(Math.abs(x - startX), Math.abs(y - startY));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment on top of this two lines of math operations (not that is unclear what they do), it will help people reading the code for the first time a lot, to understand the reasoning and why we are doing it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let put it in 2 lines and better for others to modify it in the future.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, feel free to suggest a different explanation, comments, etc...
Also, I added the animation speed which is 1 pixel/ms. It will make it easier to modify it you think that another value matches better the native bahaivour.

const scrollTime = Math.max(
MIN_SCROLL_TIME,
Math.min(MAX_SCROLL_TIME, maxDistance)
);

// scroll looping over a frame
step({
scrollable: scrollable,
Expand All @@ -229,7 +236,8 @@ function polyfill() {
startX: startX,
startY: startY,
x: x,
y: y
y: y,
scrollTime
});
}

Expand All @@ -248,14 +256,14 @@ function polyfill() {
arguments[0].left !== undefined
? arguments[0].left
: typeof arguments[0] !== 'object'
? arguments[0]
: w.scrollX || w.pageXOffset,
? arguments[0]
: w.scrollX || w.pageXOffset,
// use top prop, second argument if present or fallback to scrollY
arguments[0].top !== undefined
? arguments[0].top
: arguments[1] !== undefined
? arguments[1]
: w.scrollY || w.pageYOffset
? arguments[1]
: w.scrollY || w.pageYOffset
Comment thread
luispuig marked this conversation as resolved.
Outdated
);

return;
Expand Down