Is there a way to speed up the behavior speed of scrollTo
?
I had a stab in the dark at speed
and duration
but don\'t work!
Working solution using Promise:
function scrollDelay(ms) {
return new Promise(res => setTimeout(res, ms));
}
document.getElementById("slow-scroll-demo-button").onclick = async function() {
for (var y = 0; y <= 4200; y += 100) {
window.scrollTo({top: y, behavior: 'smooth'})
await scrollDelay(100)
}
}
Trick to introduce delay in scrolling:
Create an async function
called scrollDelay()
that spends time by calling a promise
Call the scrollDelay
along with scrollTo
in a for
loop