Skip to content

Commit

Permalink
Ovveride scroll behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
elirehema committed Feb 27, 2021
1 parent 86c313f commit 0ed71b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
32 changes: 0 additions & 32 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,8 @@ export default {
router: {
base: process.env.NODE_ENV === "production" ? "/docs/" : "/",
routerNameSplitter: "/",
scrollBehavior: async function(to, from, savedPosition) {
if (savedPosition) {
return savedPosition;
}

const findEl = async (hash, x = 0) => {
return (
document.querySelector(hash) ||
new Promise(resolve => {
if (x > 50) {
return resolve(document.querySelector("#app"));
}
setTimeout(() => {
resolve(findEl(hash, ++x || 1));
}, 100);
})
);
};

if (to.hash) {
let 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 };
}

},


loadingIndicator: {
name: 'pulse',
color: ' #00A756',
Expand Down
30 changes: 23 additions & 7 deletions router.scrollBehavior.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
export default function (to, from, savedPosition) {
if (to.hash) {
return {
selector: to.hash
// , offset: { x: 0, y: 10 }
}
export default function(to, from, savedPosition) {
const defaultPosition = false;
const scrollTopPosition = { x: 0, y: 0 };

let position = defaultPosition;
console.log({ to, from });
if (savedPosition) {
position = savedPosition;
} else if (to.matched.length < 2) {
position = scrollTopPosition;
} else if (to.matched.some(child => child.components.default.options.scrollToTop)) {
position = scrollTopPosition;
}

return new Promise(resolve => {
window.$nuxt.$once('triggerScroll', () => {
if (to.hash && document.querySelector(to.hash)) {
position = { selector: to.hash };
}
}

resolve(position);
});
});
}

0 comments on commit 0ed71b8

Please sign in to comment.