Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next route opens not from the top of a page (scrolls lower). Why?

Have anyone faced the same problem? I'm using iron router with template-level subscription.
For example. I have a long page "list of items" where I can scroll down. Then I click on one of the items somewhere at the bottom and next template renders lower than it should be.
Imagine that you search on youtube, scroll down results and then you click on a video snippet but it opens not from the top but lower so you need to scroll back to top to see the video.


I've tried to put "scroll to top" script into onRendered callback but this "jump" is recognizable with a naked eye. So it become even worse.


(update) I've found this solution for now:

Router.onBeforeAction(function() {
  $(window).scrollTop(0);
  this.next();
});
like image 758
Dmitry Avatar asked Dec 24 '22 16:12

Dmitry


1 Answers

If you are using FlowRouter, you can easily add this on a triggersEnter route definition:

const publicRoutes = FlowRouter.group({
  name: 'public',
  triggersEnter: [() => {
    window.scrollTo(0, 0);
  }],
});
like image 121
Mark Shust at M.academy Avatar answered Dec 27 '22 05:12

Mark Shust at M.academy