How to automatically scroll to top after page reload?


#1

Hi There,

I’ve been stumbling upon something very basic.

How does the page automatically scroll to top after reload or clicking on a new route?

I’m using iron router

okgrow:router-autoscroll is not working in chrome as seen here:

I found this snippet in this forum but it’s not working, either:

Tracker.autorun(function () {
	var current = Router.current();
	Tracker.afterFlush(function () {
		// Scroll window to top, like a real page reload.
		window.scrollTo(0, 0);

		// If the menu is currently open, collapse it.
		if ($('.navbar .navbar-collapse.collapse.in').length > 0) {
			$('.navbar .navbar-toggle').click();
		}
	});
});

Any help highly appreciated


#2

Looks like you’re using jQuery, in which case you can do:

$(elem).scrollTop( 0 );

where elem is a selector of the container you want to scroll to the top of. You should be able to call this in an autorun, onRendered, etc.