Thursday, December 7, 2017

Anchor tag slow scroll using jquery open direct url

below Jquery code should work for scroll anchor tag from URL

jQuery(function() {
  jQuery('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = jQuery(this.hash);
      target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        jQuery('html,body').animate({
          scrollTop: target.offset().top - 125 //offsets for fixed header
        }, 1000);
        return false;
      }
    }
  });
  //Executed on page load with URL containing an anchor tag.
  if(jQuery(location.href.split("#")[1])) {
      var target = jQuery('#'+location.href.split("#")[1]);
      if (target.length) {
        jQuery('html,body').animate({
          scrollTop: target.offset().top - 140 //offset height of header here too.
        }, 1000);
        return false;
      }
    }
});

1 comment: