Scroll Automatically to the Bottom of the Page

后端 未结 24 2462
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 05:22

Consider I have a list of questions. When I click on the first question, it should automatically take me to the bottom of the page.

For a matter of fact, I do know

相关标签:
24条回答
  • 2020-11-22 06:03

    You can use this function wherever you need to call it:

    function scroll_to(div){
       if (div.scrollTop < div.scrollHeight - div.clientHeight)
            div.scrollTop += 10; // move down
    
    }
    

    jquery.com: ScrollTo

    0 讨论(0)
  • 2020-11-22 06:04

    So many answers trying to calculate the height of the document. But it wasn't calculating correctly for me. However, both of these worked:

    jquery

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

    or just js

        window.scrollTo(0,9999);
    
    0 讨论(0)
  • 2020-11-22 06:05

    You can use this to go down the page in an animation format.

    $('html,body').animate({scrollTop: document.body.scrollHeight},"fast");
    
    0 讨论(0)
  • 2020-11-22 06:06

    You may try Gentle Anchors a nice javascript plugin.

    Example:

    function SomeFunction() {
      // your code
      // Pass an id attribute to scroll to. The # is required
      Gentle_Anchors.Setup('#destination');
      // maybe some more code
    }
    

    Compatibility Tested on:

    • Mac Firefox, Safari, Opera
    • Windows Firefox, Opera, Safari, Internet Explorer 5.55+
    • Linux untested but should be fine with Firefox at least
    0 讨论(0)
  • 2020-11-22 06:07

    If you want to scroll entire page to the bottom:

    var scrollingElement = (document.scrollingElement || document.body);
    scrollingElement.scrollTop = scrollingElement.scrollHeight;
    

    See the sample on JSFiddle

    If you want to scroll an element to the bottom:

    function gotoBottom(id){
       var element = document.getElementById(id);
       element.scrollTop = element.scrollHeight - element.clientHeight;
    }
    

    And that's how it works:

    Ref: scrollTop, scrollHeight, clientHeight

    UPDATE: Latest versions of Chrome (61+) and Firefox does not support scrolling of body, see: https://dev.opera.com/articles/fixing-the-scrolltop-bug/

    0 讨论(0)
  • 2020-11-22 06:08

    If any one searching for Angular

    you just need to scroll down add this to your div

     #scrollMe [scrollTop]="scrollMe.scrollHeight"
    
       <div class="my-list" #scrollMe [scrollTop]="scrollMe.scrollHeight">
       </div>
    
    0 讨论(0)
提交回复
热议问题