December 27, 2017
I hate anchor tags, and I hate even more that there isn’t an easy point / click way for them to be added into a website using the WordPress editor.
Nonetheless, clients request them a lot, and they don’t like the “jumping” that they cause when the anchor links are on the same page.
Copy and paste this function into your theme’s jQuery file, and it will activate smooth transitions for all anchor tags via the name attribute upon click.
$('a').click(function(){
$('html, body').animate({
scrollTop: $('[name="' + $.attr(this, 'href').substr(1) + '"]').offset().top
}, 500);
return false;
});
Additional Example:
$("#button").click(function() {
$([document.documentElement, document.body]).animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 2000);
});