Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS for smooth scroll to the bottom of the page

I have a JS to make a smooth scroll from the bottom of the page to the top with this and it works:

<script>

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

But now I want to make a smooth scroll from the top to the bottom, I tried it with this:

 <script>

     $("a[href='#footer']").click(function() {
     $("html, body").animate({ scrollToBottom: 0 }, "slow");
     return true;
  });
</script>`

It doesn't work, it's not a smooth scroll. Does anyone know what's wrong with this?

like image 755
user3597784 Avatar asked May 24 '14 09:05

user3597784


People also ask

How do I scrollTo the bottom of the page?

Of course, you can also click and drag the scroll bar on the side of the page, but that's a slow and imprecise option–especially if you're using a laptop touchpad. No, by far the best way to jump to the top or bottom of a Web page is by tapping your Home or End key, respectively.

How do you scroll down in JavaScript?

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

How do I scrollTo the bottom of an element?

Use element. scrollintoview() to Scroll to Bottom of Div in JavaScript. The Element. scrollIntoView() method will scroll an element to be visible to the user.


2 Answers

With pure JS:

 window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' })

and to the top as:

 window.scrollTo({ top: 0, behavior: 'smooth' })
like image 169
Banzy Avatar answered Oct 17 '22 10:10

Banzy


There is no such thing as scrollToBottom. Try this:

$("html, body").animate({ scrollTop: document.body.scrollHeight }, "slow");
like image 34
dfsq Avatar answered Oct 17 '22 08:10

dfsq