When using iron-router to scroll to a hash using an anchor button referencing the id of the next section, such as this:
<a class="button" href="{{pathFor 'home' hash='about'}}">
iron-router happily takes us to the about section the first time the button is clicked.
if you scroll back up using the mouse and click the same button a second time, no scrolling takes place.
I presume this is because the destination is apparently the same as the current router location, hence no reaction is triggered.
How can I force a reaction?
I've tried clearing the hash in the window.location in an override to the scrollToHash function:
Router._scrollToHash = function(hash) {
var section = $(hash);
if (section.length) {
var sectionTop = section.offset().top;
$("html, body").animate({
scrollTop: sectionTop
}, "slow");
}
window.location.hash = '';
};
And this allows a second click but no more, which has me puzzled.
Setting window.location.hash to a space rather than an empty string works:
Router._scrollToHash = function(hash) {
var section = $(hash);
if (section.length) {
var sectionTop = section.offset().top;
$("html, body").animate({
scrollTop: sectionTop
}, "slow");
}
window.location.hash = ' ';
};
Probably setting it to a non-existent hash would also.
In addition to cover cases where there is a menu link that can be activated from a second page to take you to the hash you need this package:
meteor add okgrow:iron-router-autoscroll
Used in combination it covers every case I have thought of.