chihung
3/27/2019 - 2:25 AM

add smooth-scroll (smooth scroll) on another page

/*
 *add smooth-scroll on another page
 * ----------------------------------------------- */
$(document).ready(function() {
  $(window).bind('hashchange', function(event) {
    var tgt = location.hash.replace(/^#\/?/, '');

    if (document.getElementById(tgt)) {
      $.smoothScroll({
        scrollTarget: '#' + tgt,
        offset: -10,
      });
    }
  });

  $(window).trigger('hashchange');
});
/*
 *
 * ----------------------------------------------- */
jQuery(function ($) {
  if (typeof $.smoothScroll !== 'function') {
    return false;
  }

  var reSmooth = /^#sm-/;
  var id;

  window.onload = function () {
    if (reSmooth.test(location.hash)) {
     id = '#' + location.hash.replace(reSmooth, '');

      console.log(id);

      var offset = (isMobile()) ? -40 : -90;

      var $id = $(id);
      var offsetSm = $id.data('offset-sm');
      var offsetMd = $id.data('offset-md');

      if (isMobile() && offsetSm) {
        offset = offsetSm;
      } else if (offsetMd) {
        offset = offsetMd;
      }

      $.smoothScroll({
        scrollTarget: id,
        offset: offset,
        easing: 'easeInOutCubic'
      });
    }
  }();
});