Created
October 2, 2020 01:14
-
-
Save gdamjan/6f1db53d341c392984fcc88cb3f2066b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { beforeUpdate, afterUpdate } from 'svelte'; | |
let autoscroll = true; | |
let prevHeight = 0; | |
let footer = document.querySelector('footer'); | |
beforeUpdate(() => { | |
prevHeight = document.body.scrollHeight; | |
// autoscroll if the footer is visible | |
let footerTop = footer?.offsetTop ?? document.body.scrollHeight; | |
autoscroll = window.scrollY + window.innerHeight >= footerTop; | |
console.log(footerTop, window.scrollY, window.innerHeight); | |
}); | |
afterUpdate(() => { | |
if (autoscroll) { | |
let scrollDiff = document.body.scrollHeight - prevHeight; | |
window.scrollBy({behavior:"smooth", top: scrollDiff}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment