Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mostafa6765/178f027aaa51773cf29bffeca3d7c1a6 to your computer and use it in GitHub Desktop.
Save mostafa6765/178f027aaa51773cf29bffeca3d7c1a6 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