Skip to content

Instantly share code, notes, and snippets.

@alrnz
Last active May 8, 2019 08:53
Show Gist options
  • Save alrnz/5f12761828519f19d207 to your computer and use it in GitHub Desktop.
Save alrnz/5f12761828519f19d207 to your computer and use it in GitHub Desktop.
smoothAnkerScroll #JS
(function($) {
$(document).ready(function(){
function smoothAnkerScroll(){
try{
var hash = window.location.hash;
var target = hash.replace('#to-', '');
if(target != ''){
target = $('#' + target);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
$('a[href*=#]:not([href=#])').click(function() {
if( ! $(this).hasClass('fancybox') ){
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;
}
}
}
});
} catch (e) {
if (typeof console === "object") { console.log("There is an error.. sorry :("); }
}
}
smoothAnkerScroll();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment