Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery scroll to bottom of page

Tags:

jquery

Im using the following to scroll to the top of a page when you click a certain link.

$('.myLinkToTop').click(function () {
    $('html, body').animate({scrollTop:0}, 'slow');
    return false;
});

I want to make another link that scrolls to the bottom of the page. The following is working OK. I think it tries to scroll 1000px down the page, so if the page is shorter then it scrolls faster than it should, and if the page is taller then it wont go all the way to the bottom. How can I replace '1000' with the window height? Thanks

$('.myMenuLink').click(function () {
    $('html, body').animate({scrollTop:1000}, 'slow');
    return false;
});

I know that this code jumps to the bottom of the page, but it doenst scroll smoothly like I need:

$(document).scrollTop($(document).height());
like image 915
Evanss Avatar asked Nov 27 '12 11:11

Evanss


People also ask

How do I scroll to 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 automatically to the bottom of the page using jQuery?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.

How do you scroll to the bottom of the div on load?

Use scrollTop and scrollHeight to Scroll to the Bottom of Div in JavaScript. A combination of scrollTop and scrollHeight can cause an element to scroll to the bottom because scrollTop determines the number of pixels for a vertical scroll. In contrast, scrollHeight is the element's height (visible and non-visible parts) ...

How do you scroll down in JavaScript?

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


2 Answers

Your requirement to animate and move to bottom of document can be achieved by the code below

HTML

<html>
<head>
</head>
<body>
    <div style="height:1500px">
        <button class="myLinkToTop" id="but1" >1</button>
    </div>
        <button class="myMenuLink" id="but1" >2</button>
</body>
</html>

JS

$('.myLinkToTop').click(function () {
    $('html, body').animate({
        scrollTop: $(document).height()
    }, 'slow');
    return false;
});

$('.myMenuLink').click(function () {
    $('html, body').animate({
        scrollTop:0
    }, 'slow');
    return false;
});

Refer to this link

http://jsfiddle.net/q6Wsp/6/

like image 134
Sridhar Narasimhan Avatar answered Oct 06 '22 00:10

Sridhar Narasimhan


You need to substract the viewport height from the scrollHeight :

$('#goToBottom').click(function(){

  var WH = $(window).height();  
  var SH = $('body').prop("scrollHeight");
  $('html, body').stop().animate({scrollTop: SH-WH}, 1000);

}); 
body{height:2000px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="goToBottom">GO TO BOTTOM</button>
like image 27
Roko C. Buljan Avatar answered Oct 06 '22 01:10

Roko C. Buljan