Forked from
websites / monnaie-libre-fr
1573 commits behind the upstream repository.
-
Emmanuel Salomon authoredEmmanuel Salomon authored
router.scrollBehavior.js 683 B
export default async function (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
}
const findEl = (hash, x) => {
return (
document.querySelector(hash) ||
new Promise((resolve, reject) => {
if (x > 50) {
return resolve()
}
setTimeout(() => {
resolve(findEl(hash, ++x || 1))
}, 100)
})
)
}
if (to.hash) {
const el = await findEl(to.hash)
if ('scrollBehavior' in document.documentElement.style) {
return window.scrollTo({ top: el.offsetTop, behavior: 'smooth' })
} else {
return window.scrollTo(0, el.offsetTop)
}
}
return { x: 0, y: 0 }
}