Search code examples
meteoriron-router

Animation between hashtag-changes in iron:router


On my website I navigate in my navbar between my-website/#about and my-website/#contact

With the new version of iron:router the router jumps automatically to the according anchors, but I want to add an animation between these two routes instead of a hard jump that iron:router provides.

Any ideas where to hook in?

Greets


Solution

  • There's a hidden Router._scrollToHash method in iron:router that is called when you click on an anchor with a hash (#id), you can override the default behavior to animate smoothly from the current location to the element using this code :

    Router._scrollToHash = function(hash) {
      var section = $(hash);
      if (section.length) {
        var sectionTop = section.offset().top;
        $("html, body").animate({
          scrollTop: sectionTop
        }, "slow");
      }
    };
    

    Don't hesitate to dive into iron:router code when stumbling upon problems like this one, the source is very readable.

    https://github.com/EventedMind/iron-router/blob/e403a72c596bc1f67dc635f07c742122edf29202/lib/router_client.js#L153