Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Last active August 20, 2019 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manchumahara/fdf41174a6ed79321978db91b85f8779 to your computer and use it in GitHub Desktop.
Save manchumahara/fdf41174a6ed79321978db91b85f8779 to your computer and use it in GitHub Desktop.
(function($) {
$(document).ready(function($) {
//hash link click
$('a[href*="#"]').on('click', function(e) {
e.preventDefault();
//console.log('hash clicked'); //uncomment this link to understand if click is working
var $this = $(this);
var $hash = $this.attr('href');
var $link_hash = $this.prop("hash");
//console.log($this.prop("hash"));
if ($link_hash != '' && $($link_hash).length > 0) {
//2# Hash link click for same page
$('html, body').animate({
//scrollTop: $($(this).attr('href')).offset().top
scrollTop: parseInt($($link_hash).offset().top - 90)
}, 900, 'swing');
} else if ($hash != $link_hash && $hash != '') {
//3# Hash based external link
window.location = $hash;
}
});
});
//1# Jump based on window hash url
var windowhash = window.location.hash;
if (windowhash != '' && $(windowhash).length > 0) {
$('html, body').animate({
scrollTop: parseInt($(windowhash).offset().top - 90)
}, 900, 'swing');
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment