Skip to content

Instantly share code, notes, and snippets.

@mondaychen
Created February 11, 2016 00:10
Show Gist options
  • Save mondaychen/352a1d80e6f681d7dc90 to your computer and use it in GitHub Desktop.
Save mondaychen/352a1d80e6f681d7dc90 to your computer and use it in GitHub Desktop.
Page Scroll Workaround for Single-Page Apps
var $container = $('#container');
...
// when some route is matched
var view = new AnotherPage();
$container.html(view.render().el);
window.scrollTo(0, 0);
$container.html(el)
// jump to top if needed
if(historyManager.isUserJump(Backbone.history.fragment)) {
window.scrollTo(0, 0)
}
// make body long enough to help browser jump to the right position
$body.css('min-height', '99999px')
// set dom
$container.html(el)
// jump to top if needed
if(historyManager.isUserJump(Backbone.history.fragment)) {
window.scrollTo(0, 0)
}
// after 0.1s set min-height to a normal value
// hopefully no one will notice that
_.delay(function() {
$body.css('min-height', '450px')
}, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment