andru26
3/30/2017 - 9:48 PM

Scroll to element via data target

Scroll to element via data target

//scroll to element function
$.fn.scrollView = function () { 

  var 
    siteHeader = "#company-header",
    offset = 15,
    headerHeight = 0;

  if($(siteHeader).length > 0){
    headerHeight = $(siteHeader).outerHeight();
  }
  offset = offset + headerHeight;
  return this.each(function () {
  $('html, body').animate({
    scrollTop: $(this).offset().top - offset
  }, 600);
  });

}

// scroll to element via data scroll-to 
function scrollToElement(e){
  e.preventDefault();
  var 
    $this = $(this), 
    target = $this.data("scroll-to");
  $(target).scrollView();
}   
$("body").on("click","[data-scroll-to]", scrollToElement);