Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Created October 2, 2020 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdamjan/6f1db53d341c392984fcc88cb3f2066b to your computer and use it in GitHub Desktop.
Save gdamjan/6f1db53d341c392984fcc88cb3f2066b to your computer and use it in GitHub Desktop.
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