Computer Magic Logo
Scroll to Top

Friday, January 22, 2016

Published by Aristotelis Pitaridis

We have an element with id "GoToElement" which will start the scrolling.

$("#GoToElement").click(function() {
    $('html, body').animate({
        scrollTop: 0
    }, 2000);
    return false;
});

If we have a lot of links which will be used as a go to top link then we can create the code once for all the links. We can set the href property of these links to be equal to "#top" and use the following code.

$("a[href='#top']").click(function() {
    $('html, body').animate({
        scrollTop: 0
    }, 2000);
    return false;
});