javascript tutorial - [Solved-5 Solutions] Scroll to the top of the page using JavaScript/jQuery - javascript - java script - javascript array



Problem :

How to scroll to the top of the page using JavaScript/jQuery ?

Solution 1:

If we don't need the change to animate then you don't need to use any special plugins - I'd just use the native JavaScript window.scrollTo method -- passing in 0,0 will scroll the page to the top left instantly.

window.scrollTo(x-coord, y-coord);
click below button to copy the code. By JavaScript tutorial team

Parameters

  • x-coord is the pixel along the horizontal axis.
  • y-coord is the pixel along the vertical axis.

Solution 2:

If we do want smooth scrolling, try something like this:

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

click below button to copy the code. By JavaScript tutorial team

Solution 3:

Try this to scroll on top

<script>
 $(document).ready(function(){
    $(window).scrollTop(0);
});
</script>
click below button to copy the code. By JavaScript tutorial team

Solution 4:

We don't need jQuery to do this. A standard HTML tag will suffice...

<div id="jump_to_me">
    blah blah blah
</div>

<a target="#jump_to_me">Click Here To Destroy The World!</a>
click below button to copy the code. By JavaScript tutorial team

Solution 5:

<script>

  $("a[href='#top']").click(function() {
     $("html, body").animate({ scrollTop: 0 }, "slow");
     return false;
  });
</script>
click below button to copy the code. By JavaScript tutorial team

in html

<a href="#top">go top</a>
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Scroll to the top of the page using JavaScript/jQuery