Animate Scroll to Page Anchor with jQuery

Scripts for ‘scrolling to an id’ (such as jQuery.scrollTo) have unnecessary customization options and do not handle all page links, pointing to anchors, on their own. (or is it just me?)

Solution

jQuery('a[href*=\"#\"]').on('click', function(e) {

    e.preventDefault()

    jQuery('html, body').animate(
    {
      scrollTop: jQuery(jQuery(this).attr('href')).offset().top,
    }
    ,500
    ,'linear'
    )
    
});

 

Was this post helpful?