Skip to content
Sep 18 / Nizam Sayeed

Go to top of page using jQuery

This one liner jQuery snippet will force your browser to go to the top of your page:

$('html, body').animate({ scrollTop: 0 }, 0);

If you want to add some smooth scrolling:

$('html, body').animate({ scrollTop: 0 }, 'slow');
  • http://www.Blockstopper.co.uk Blockstopper

    hiya,

    Do you have to add an element that the user can click on?
    I ask because I have jQuery installed: jquery-1.3.2.min.js
    ..and javScript enabled.

    I have other jQuery effects working on the same page.

    I tried the following:

    TOP

    $(“h3″)
    $(‘html, body’).animate({ scrollTop: 0 }, ‘slow’);

    Do you know where I’m going wrong?

    Many thanks,

    Blockstopper

    • dVICIOUS

      Try
      $(“h3″).click(function(){
                $(‘html, body’).animate({ scrollTop: 0 }, ‘slow’);
      })

      That should work. Although that would make all h3 things take the user to the top of the page. Pehaps give your item the ID “TopPage” then instead of $(‘h3′) you could use $(‘#TopPage’)

      Urgh just found the reply button

  • Jisjad

    Thank you.

  • http://whofindwho.com Canmasu

    thanks this is cool. workable code

  • Nicole

    That was exactly what I needed, thanks so much!

  • http://hirephpwebdeveloper.com cerah

    Can you please tell me that how can i scroll to particular div using .scrollTop.

    Thanks

    • http://www.podoba.cz cabal

      $(“.yourElementClass”).click(function(){
      $(‘.yourDivName).animate({ scrollTop: 0 }, ’slow’);
      });

  • http://thinkdiff.net Mahmud Ahsan

    Just found the trick. Thank You.

  • http://www.Mokumax.com Craig Kelley

    Perfect! Thanks.

  • dVICIOUS##

    #Blockstopper
    Try
    $(“h3″).click(function(){
              $(‘html, body’).animate({ scrollTop: 0 }, ‘slow’);
    })

    That should work. Although that would make all h3 things take the user to the top of the page. Pehaps give your item the ID “TopPage” then instead of $(‘h3′) you could use $(‘#TopPage’)

  • Curve1322

    THANK YOU SO MUCH!!!!!! THIS worked like a charm!!!!!!!

  • http://globodisenios.com.ar/ Gerardo Perrucci

    Thanks I was looking one easy solution and find it here.
    Gerardo