I\'ve bean searching for this for a few hours now and I have no solution. I want a smooth scroll to the top of the page. I already have smooth scrolling to separate anchors
Came up with this solution:
function scrollToTop() {
let currentOffset = window.pageYOffset;
const arr = [];
for (let i = 100; i >= 0; i--) {
arr.push(new Promise(res => {
setTimeout(() => {
res(currentOffset * (i / 100));
},
2 * (100 - i))
})
);
}
arr.reduce((acc, curr, index, arr) => {
return acc.then((res) => {
if (typeof res === 'number')
window.scrollTo(0, res)
return curr
})
}, Promise.resolve(currentOffset)).then(() => {
window.scrollTo(0, 0)
})}