Javascript, jQuery | Type: JavascriptSmoothScroll to Top

For object with class “scroller”; also note define object width for fixed, as it seems to disregard previously set width.

<script>
jQuery(function ($) {
$('a[href*="#"]') // Select all links with hashes
.not('[href="#"]')// Remove links that don't actually link to anything
.not('[href="#0"]')
click(function(event) {
    if (
        location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname // On-page links
    ) {
        var target = $(this.hash); // Figure out element to scroll to
        target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
        if (target.length) { // Does a scroll target exist?
            event.preventDefault();// Only prevent default if animation is actually gonna happen
            $('html, body').animate({
            scrollTop: target.offset().top
            }, 650, function() {
                var $target = $(target); // Callback after animation; Must change focus!
                $target.focus();
                if ($target.is(":focus")) { // Checking if the target was focused
                    return false;
                } else {
                    $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
                    $target.focus(); // Set focus again
                }
            });
        }
    }
    });
});
});
</script>

Reference Links

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept