Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for resetting scroll position between pages?

So even the Polymer website has this issue. This is an SPA-world problem.

Repro: Go to http://www.polymer-project.org/docs/elements/core-elements.html, click on e.g. core-ajax on the left and scroll down to the middle, then goto core-xhr. Note the scroll position.

What are some best practices considering that I a) want to avoid behavior like the above, but also b) want to preserve the scroll position for when I use the back arrow to goto a page I've already been? It'd be nice if core-pages had support built in.

like image 562
David Notik Avatar asked Jul 30 '14 21:07

David Notik


1 Answers

This might work as a workaround. You can use the fire event in polymer, once something fire the event you listen to the call and force the page to scroll to top. At least this solved my problem with core-animated-pages transition: slide-from-right.

Polymer

this.fire('scroll-top')

Index.html

document.addEventListener('scroll-top', function(){
    // Access the main core-header-panel 
    var scaffold = document.querySelector('core-scaffold');
    var scrollArea = scaffold.shadowRoot.querySelector('core-header-panel');
    scrollArea.scroller.scrollTop = 0; // Scroll to top
    }
)
like image 94
wirlez Avatar answered Oct 19 '22 16:10

wirlez