Created
April 8, 2023 17:25
-
-
Save cyrillsemenov/bc7d1c032cee3d2a2c2ca5314f0a5ce7 to your computer and use it in GitHub Desktop.
Scroll to position from the start of page
This file contains hidden or 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
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