Search code examples
javascriptjquerydelayscrolltop

Simple Scroll to Top transition not working


Trying to make a simple scroll to top button that smoothly scrolls up and I thought this would make it work.

Right now all it does is make the page shoot right to the top so it is working at least sort of correct just the delay isn't working, I'm no expert on jQuery/Javascript and can't quite figure it out.

$('.top').click(function(){

    var scrollPosition = $('.scrollfix').scrollTop();

    while (scrollPosition > 0 ) {
        $('.scrollfix').delay(1).scrollTop(scrollPosition);
        scrollPosition--;
    }
});

You can see it in use here: http://jsfiddle.net/JamesKyle/8H7hR/101/


Solution

  • $('.top').click(function(){
        $('.scrollfix').animate({scrollTop : 0},1000);
    });