Backbone.js - Back button that restores the scroll position

I have a web application written in backbone.js. Say I have a search results page and the user scrolls to the bottom of the search results. The user clicks on the last search result, and a page is displayed for this result.

I want to support the back button, and when the user clicks back, the router starts and re-displays the search page. The problem is that the search page is re-displayed, it scrolls up from the top.

What is the best way to restore results to their original position?

+4
source share
1 answer

Consider tracking the scroll position in a view. Then on render or let route change the position of the page

 View = Backbone.View.extend({ initialize: function () { var self = this; $( window ).on( 'scroll', function () { self.position = this.pageYOffset; }); }, render: function () { // render code if ( this.position ) { window.scrollBy( 0, this.position ); } } }); 
+1
source

All Articles