From 18d5182b83c46eb508d9b512b44776ecb7293d4e Mon Sep 17 00:00:00 2001 From: Luis Puig Date: Thu, 26 Nov 2020 19:23:06 +0100 Subject: [PATCH 1/3] Implement relative scroll time from distance --- src/smoothscroll.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/smoothscroll.js b/src/smoothscroll.js index 6faa8f7..ac7be27 100644 --- a/src/smoothscroll.js +++ b/src/smoothscroll.js @@ -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 = { @@ -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; @@ -221,6 +222,12 @@ function polyfill() { method = scrollElement; } + const maxDistance = Math.max(Math.abs(x - startX), Math.abs(y - startY)); + const scrollTime = Math.max( + MIN_SCROLL_TIME, + Math.min(MAX_SCROLL_TIME, maxDistance) + ); + // scroll looping over a frame step({ scrollable: scrollable, @@ -229,7 +236,8 @@ function polyfill() { startX: startX, startY: startY, x: x, - y: y + y: y, + scrollTime }); } @@ -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 ); return; From 6d8e7e2720367603b2aaa95a01567f445ce15692 Mon Sep 17 00:00:00 2001 From: Luis Puig Date: Mon, 30 Nov 2020 09:21:33 +0100 Subject: [PATCH 2/3] Improve code explinations --- src/smoothscroll.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/smoothscroll.js b/src/smoothscroll.js index ac7be27..fd5d9dd 100644 --- a/src/smoothscroll.js +++ b/src/smoothscroll.js @@ -222,10 +222,17 @@ function polyfill() { method = scrollElement; } - const maxDistance = Math.max(Math.abs(x - startX), Math.abs(y - startY)); - const scrollTime = Math.max( + // 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); + // Configure the speed animation in pixels/ms + const animationSpeed = 1; + // Calculate the time needed for the scroll animation + const scrollTime = + Math.max( MIN_SCROLL_TIME, - Math.min(MAX_SCROLL_TIME, maxDistance) + Math.min(MAX_SCROLL_TIME, maxDistanceToScroll * animationSpeed) ); // scroll looping over a frame @@ -256,14 +263,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 ); return; From 26c52dbe40de9306901c21cd50c994443399671b Mon Sep 17 00:00:00 2001 From: Luis Puig Date: Mon, 30 Nov 2020 09:34:01 +0100 Subject: [PATCH 3/3] Move SCROLL_SPEED to globals --- src/smoothscroll.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/smoothscroll.js b/src/smoothscroll.js index fd5d9dd..603374d 100644 --- a/src/smoothscroll.js +++ b/src/smoothscroll.js @@ -18,6 +18,8 @@ function polyfill() { var Element = w.HTMLElement || w.Element; 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 = { @@ -226,13 +228,11 @@ function polyfill() { const distanceToScrollX = Math.abs(x - startX); const distanceToScrollY = Math.abs(y - startY); const maxDistanceToScroll = Math.max(distanceToScrollX, distanceToScrollY); - // Configure the speed animation in pixels/ms - const animationSpeed = 1; // Calculate the time needed for the scroll animation const scrollTime = Math.max( MIN_SCROLL_TIME, - Math.min(MAX_SCROLL_TIME, maxDistanceToScroll * animationSpeed) + Math.min(MAX_SCROLL_TIME, maxDistanceToScroll * SCROLL_SPEED) ); // scroll looping over a frame