From dbba62e86a0a7abbefe8c2f4cc80ba65060d9c68 Mon Sep 17 00:00:00 2001 From: tristantbg Date: Wed, 3 Apr 2019 22:15:04 +0200 Subject: [PATCH] Scroll to element that are in bottom of the page Prevent a brutal stop animation if a targeted element is at the very bottom of the page. --- src/jump.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/jump.js b/src/jump.js index a874be1..f04b18d 100644 --- a/src/jump.js +++ b/src/jump.js @@ -32,7 +32,15 @@ const jumper = () => { // element offset helper function top (element) { - return element.getBoundingClientRect().top + start + // get max scroll value + const windowHeight = (window.innerHeight || document.documentElement.clientHeight); + const documentHeight = document.body.clientHeight; + const max = documentHeight - windowHeight - offset; + + // get element scroll top + const top = element.getBoundingClientRect().top + start; + + return Math.min(element.getBoundingClientRect().top + start, max); } // rAF loop helper