umerata
8/11/2017 - 10:20 AM

Smooth Scroll for links on same page | label-js

Smooth Scroll for links on same page

// Smooth scroll effect for anhors on a same page

var isLinkForSamePage ;
  $(document).on('click', 'a[href*="#"]', function (e) {
    isLinkForSamePage = (this.pathname == location.pathname || ('/' + this.pathname == location.pathname)) && this.hash.length  && location.hostname == this.hostname;
      if (isLinkForSamePage && $(this.hash).length) {
          scrollToPos()
      }
  })



$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
}); 


function scrollToWidget (hash) {
    var $targetElement,
        topOffset;
    if (hash.length && (hash.indexOf("#") !== -1)) {
        hash = hash.substring(hash.lastIndexOf("#") + 1);
        $targetElement = $("[data-widget-id='" + hash + "']");
        if ($targetElement.length) {
            topOffset = Math.round($targetElement.offset().top) - app.getStickyElementsHeights();
            app.scrollToPos(topOffset)
        }
    }
}


//  

function scrollToPos (pos) {
    
    if (pos == $(window).scrollTop())
    return false;
    
    $("html, body").stop().animate({
        scrollTop: pos
    }, 500);
    
    return false;
}