Skip to content

Instantly share code, notes, and snippets.

@cyrillsemenov
Created April 8, 2023 17:25
Show Gist options
  • Save cyrillsemenov/bc7d1c032cee3d2a2c2ca5314f0a5ce7 to your computer and use it in GitHub Desktop.
Save cyrillsemenov/bc7d1c032cee3d2a2c2ca5314f0a5ce7 to your computer and use it in GitHub Desktop.
Scroll to position from the start of page
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function animateScroll(dest, easeAmt) {
window.scroll(0,0);
let easeIn = (p) => p < 0.5 ? Math.pow(p*2, easeAmt)/2 : p;
let easeOut = (p) => p > 0.5 ? (2 - Math.pow(2 - p*2, easeAmt))/2 : p;
await sleep(1000);
for (let i = 0; i < 1; i+=0.001) {
let progress = easeIn(easeOut(i));
window.scroll(0, dest*progress);
await sleep(1);
}
}
animateScroll(10600, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment