Inertia.js and scroll management of scrollable divs #1374
Replies: 2 comments
-
I had a similar issue and this is what I did: export const preserveScroll = (selector) => {
const scrollPos = document.querySelector(selector).scrollTop
document.addEventListener(
'inertia:navigate',
() => {
document.querySelector(selector).scrollTop = scrollPos
},
{ once: true }
)
} I add a The function will find the scroll position of the element before something happens, then re-define it to the previous position after inertia navigate event is dispatched. Obs.: You can't store the element in a variable and reuse it inside the callback, or it won't work. |
Beta Was this translation helpful? Give feedback.
-
Modified version of @JoaoHCopetti's script that worked for my use case: const preserveScroll = () => {
if (!window.gpScrollRegions) window.gpScrollRegions = {};
const scrollRegions = document.querySelectorAll('[scroll-region]');
scrollRegions.forEach(region => {
window.gpScrollRegions[region.getAttribute('scroll-region')] = region.scrollTop;
});
document.addEventListener(
'inertia:finish',
() => {
setTimeout(() => {
const scrollRegions = document.querySelectorAll('[scroll-region]');
scrollRegions.forEach(region => {
region.scrollTop = window.gpScrollRegions[region.getAttribute('scroll-region')] ?? 0;
});
}, 0);
},
{ once: true }
);
}; Encountered multiple issues:
Unsure what's the cause |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I think I may misunderstand scroll management with Inertia.js and scrollable divs. Maybe somebody can point me in the right direction.
I started off without scrollable divs. And using "preserve-scroll" on links works fine and remembers the scroll position in the document body.
Then I switched to a new layout where the height is always 100% of the screen. And the logical columns of the application scroll automatically vertically. Slack-app style.
Clicking on Inertia links with preserve-scroll now does not restore the scroll position of the scrollable elements.
I tried a couple of things like using "scroll-region" but in the documentation it sounds as if scroll-region is not meant for preserving the scroll position but for resetting it.
This is a simplified example of my code (with Vue and Tailwind):
I also tried to add "scroll-region" to the "ul" tag. Using "preserve-state" in the link kind of does the trick but I would like to only preserve the scroll positions of all the scrollable elements after any click.
Any tipps appreciated.
Beta Was this translation helpful? Give feedback.
All reactions