Tabraiz Ali
Tabraiz Ali

Reputation: 677

go to top of the page using jquery in chrome

I'm using a ui theme called nightsky

After I scroll to the bottom of the page I want to go back to the top of the page.

What I have tried already in dev console :

  1. window.scrollTo(0, $("body").offset().top);
  2. window.scrollTo(0, $("#main").offset().top();
  3. The following:

    $('html, body').animate({
        scrollTop: $('#main').position().top },
        1000
    );
    

    seems to work in firefox not in chrome

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

I am looking for a code that can be executed in console! no on-click listeners etc...just few lines of code

Upvotes: 2

Views: 251

Answers (4)

Jignesh Kheni
Jignesh Kheni

Reputation: 1302

Try this

$(document).ready(function(){
  $('.top').on('click', function() {
     $('html,body').animate({'scrollTop':'0px'},1000);
  });
});

here is demo link : http://jsfiddle.net/jkkheni/JWJ5N/13/

Upvotes: 0

Ariana Lynn
Ariana Lynn

Reputation: 1

You can try:

<a href="#top">Click</a>

Upvotes: 0

Rahiil
Rahiil

Reputation: 110

why dont you use <a href="#header">Go to top</a>

Upvotes: 0

Shaunak D
Shaunak D

Reputation: 20626

Try jQuery .scrollTop(),

$(window).scrollTop(0)   //Or $('body').scrollTop(0)

Demo

Upvotes: 1

Related Questions