All things Shopify and commerce
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
I'm currently on Nimbus theme and have hundreds of products on each Collection page. When a user browses a collection >>clicks on a product >> then clicks the back button, the user should be brought back to the position of the product they last viewed so that they can continue browsing where they left off. However, this theme currently brings the user all the way back to the top of the collection and the user has to scroll all the way down again to go back to their position. This is an annoying and terrible user experience. Can someone provide a solution for this?
Could you please share your store URL and password [if applicable] so that I can take a look and provide you solution code.
Looking forward to hearing back from you.
Thanks
I think I should have been more clear: the issue I think only appears on IOS mobile and when the collection page is set to be on infinite scroll or "Show More" button. this URL has this setting: https://cot1gzifpty4hdgx-58770456708.shopifypreview.com
Hi Artey,
This feature should be supported by the theme by default.
I tried the Nimbus demo store. It works as you expected, have you updated to latest version of the theme?
Yes we're on the latest version. I agree with you . This needs to be supported by the theme by default. But they're saying it requires a custom code and saying we should pay extra for it.
Solution: Save and Restore Scroll Position Using JavaScript
We'll use sessionStorage to save the user's scroll position when they click a product, and then restore that position when they return to the collection page.
Step 1: Add JavaScript to Save and Restore Scroll Position
In your Shopify admin, go to Online Store > Themes > Edit Code. Then, in your theme.liquid file, just before the closing </body> tag, insert the following script:
<script>
document.addEventListener("DOMContentLoaded", function () {
// Save scroll position when clicking a product
document.querySelectorAll('a[href*="/products/"]').forEach(function(link) {
link.addEventListener("click", function () {
sessionStorage.setItem("collectionScrollPosition", window.scrollY);
});
});
// Restore scroll position if available
if (window.location.href.includes("/collections/") && sessionStorage.getItem("collectionScrollPosition")) {
window.scrollTo(0, sessionStorage.getItem("collectionScrollPosition"));
sessionStorage.removeItem("collectionScrollPosition");
}
});
</script>
Saves the vertical scroll position (window.scrollY) when a product link is clicked.
When the user returns to a collection page, the script checks for the saved scroll position and restores it.
After restoring the scroll position, the script removes the stored value to prevent it from affecting future navigation.
If your theme uses AJAX navigation or infinite scroll, you may need to adapt the script slightly. For example, you could wrap the window.scrollTo call in a setTimeout or wait for a custom event that indicates the collection has finished rendering.
With this script in place, users who view a product and then return to the collection page will land exactly where they left off, providing a smooth and intuitive browsing experience.
If you need help integrating this into a theme using dynamic content loading or if you're unsure where to place the code in your theme's structure, let me know and I’ll guide you step-by-step.
Hi there, thanks for the reply! Yes, my theme has the option for the collection page to have infinite scroll or show more button. I would like users to be able to return to their position when infinite scroll or show more setting is activated on collection pages. How do I adapt this code to work for both settings?