Skip to content
Open
Changes from all commits
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
21 changes: 18 additions & 3 deletions src/smoothscroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ function polyfill() {

// globals
var Element = w.HTMLElement || w.Element;
var SCROLL_TIME = 468;
var MIN_SCROLL_TIME = 200;
var MAX_SCROLL_TIME = 468;
// Scroll animation speed in pixels / ms
var SCROLL_SPEED = 1;

// object gathering original scroll methods
var original = {
Expand Down Expand Up @@ -174,7 +177,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 +224,17 @@ function polyfill() {
method = scrollElement;
}

// Calculate the maximum distance to scroll in pixels
const distanceToScrollX = Math.abs(x - startX);
const distanceToScrollY = Math.abs(y - startY);
const maxDistanceToScroll = Math.max(distanceToScrollX, distanceToScrollY);
// Calculate the time needed for the scroll animation
const scrollTime =
Math.max(
MIN_SCROLL_TIME,
Math.min(MAX_SCROLL_TIME, maxDistanceToScroll * SCROLL_SPEED)
);

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

Expand Down