Thursday, April 28, 2016

Scrolling to an element on a page using jQuery

// id is the id of an HTML element (without the # prefix)
//   the element with the id must not have display:none style
//   (otherwise .offset() will be null)
// fixedNavBarHeight is height of the fixed nav bar at the top
// Call this onReady handler (that will ensure target element has been loaded before the call)
function scrollTo(id, fixedNavBarHeight) {
            $('html,body').animate({ scrollTop: $("#" + id).offset().top - fixedNavBarHeight }, 'fast');
        }