Search code examples
javascriptmeteoriron-router

When I'm scrolled down and click a link, the next page starts out scrolled down


I'm sorry that I can give very little example code or details, but the truth is that it would be altogether meaningless since this is a very general case.

Whenever I'm scrolled down on a page and click a link to the next page, that page starts out scrolled down. It's not the case that if I'm at the bottom of the first page, that I will start out at the bottom of the next; it seems a bit random that way.

Noteworthy is probably that all these links are made with events that use Router.go, for instance:

Router.go('showroom', {cat: category, _id: this._id})

Is there a fix to this, or do I have to hack around the issue?


Solution

  • Simple fix : use a global onBeforeAction hook to scroll back to the top of the page when changing to any route :

    Router.onBeforeAction(function(){
      $(window).scrollTop(0);
      this.next();
    });