Ravi Aaaaaa
Ravi Aaaaaa

Reputation: 216

How to move cursor to top of the page in jQuery?

I'm using jQuery in my project. I have one div that's on the button of the page. I want to go to the top of the page when a user clicks on it.

I tried this but it goes to the bottom most position of the page:

window.scrollTo(0, document.body.scrollHeight);

How can I go to the top of the page?

Upvotes: 0

Views: 2794

Answers (2)

Karl Anderson
Karl Anderson

Reputation: 34844

Try this:

window.scrollTo(0,0);

For a nice walkthrough of how to use a button to make a page smoothly scroll back to the top with jQuery's fadein() and fadeout(), then check out Smooth Page Scroll to Top with jQuery

It also has a live demo

Upvotes: 3

James Dawson
James Dawson

Reputation: 5409

The scrollTo function takes two parameters, x position and y position. Just set y position to zero.

window.scrollTo(0, 0);

Upvotes: 1

Related Questions