
# smooth scroll to element
module.exports = ->
  script 'data-id': 'scrollTo', -> raw """
    (function() {
      window.scrollTo = function(id, duration) {
        var to = document.getElementById(id).offsetTop - 100;
        if (duration == null) { duration = 200; }

        if (duration <= 0) return;
        var difference = to - document.body.scrollTop;
        var per_tick = difference / duration * 10;

        setTimeout(function() {
          document.body.scrollTop = document.body.scrollTop + per_tick;
          if (document.body.scrollTop === to) return;
          scrollTo(id, duration - 10);
        }, 10);
      }
    })();
  """
