drifterz28
7/1/2013 - 1:44 PM

jQuery scroll to for anchor tags and "to top" buttons with push state of hash.

jQuery scroll to for anchor tags and "to top" buttons with push state of hash.

$(function () {
    $('a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        var anchorID = $(this).attr('href');
        var anchorPos = $(anchorID).offset();
        $('html, body').animate({scrollTop: anchorPos.top}, 400, function () {
            location.hash = anchorID;
        });
    });
});