Last active
May 8, 2019 08:53
-
-
Save alrnz/5f12761828519f19d207 to your computer and use it in GitHub Desktop.
smoothAnkerScroll #JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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