开发者

Retain scroll location when navigating back from another page via browser 'back' or link back?

开发者 https://www.devze.com 2023-02-27 00:26 出处:网络
I\'m having some issues with maintaining scroll bar locations. The Background I have a horizontal image browser in one of my pages. It lazy loads its images, so it can potentially scroll through thou

I'm having some issues with maintaining scroll bar locations.

The Background

I have a horizontal image browser in one of my pages. It lazy loads its images, so it can potentially scroll through thousands of images. When you click each image, it takes you to a detail page with an enlarged image and some meta-data. From this page you can also move directly through开发者_StackOverflow other detail pages in the same order that you saw them in the scroller.

edit: clarifying

There is a page with an HTML element with overflow:auto and a scroll bar. The contents of this element are very, very long. When you move away and then return to this page, I would like the scroll bar to remember the last place you were looking at.

The Goal

What I would like to do is allow the user to get back to page with the scroller without it resetting to the beginning of the scroller every time. If you use the browser's "back" button, then this works automatically, but when you use my explicit link back to the search/browse results, you start at the beginning every time. (Also, the browser 'back' button becomes less feasible once people have navigated from detail page to detail page a few times, as is possible.)

Approaches to Avoid

My previous attempt submitted a form between all of the pages that kept the scroller location in a $_POST var, but then when you want to use the back button you get that annoying popup asking you to confirm, so I want to stay away from that.

I also don't want to put it into the $_GET object, as this will mess with the SEO of the pages by having multiple urls point to the same page.

The Question

What do you think is the best way to implement this?

  • Should I use some cleverness with window.history?
  • Should I send an AJAX request with the scroll location to a script that will store the value in the $_SESSION object?
  • Something else?

Thanks in advance for your suggestions.

-K


The solution that I ended up using was to set a session var with a unique element id in each detail page, then read that session var in the browse page and use offsets to scroll directly to the element that was last examined on a detail page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号