Scroll to bottom of div?

前端 未结 30 2550
日久生厌
日久生厌 2020-11-21 11:56

I am creating a chat using Ajax requests and I\'m trying to get messages div to scroll to the bottom without much luck.

I am wrapping everything in this div:



        
相关标签:
30条回答
  • 2020-11-21 11:59

    Using jQuery, scrollTop is used to set the vertical position of scollbar for any given element. there is also a nice jquery scrollTo plugin used to scroll with animation and different options (demos)

    var myDiv = $("#div_id").get(0);
    myDiv.scrollTop = myDiv.scrollHeight;
    

    if you want to use jQuery's animate method to add animation while scrolling down, check the following snippet:

    var myDiv = $("#div_id").get(0);
    myDiv.animate({
        scrollTop: myDiv.scrollHeight
      }, 500);
    
    0 讨论(0)
  • 2020-11-21 12:01

    Here's what I use on my site:

    var objDiv = document.getElementById("your_div");
    objDiv.scrollTop = objDiv.scrollHeight;
    
    0 讨论(0)
  • 2020-11-21 12:02

    Newer method that works on all current browsers:

    this.scrollIntoView(false);
    
    0 讨论(0)
  • 2020-11-21 12:02

    Found this really helpful, thank you.

    For the Angular 1.X folks out there:

    angular.module('myApp').controller('myController', ['$scope', '$document',
      function($scope, $document) {
    
        var overflowScrollElement = $document[0].getElementById('your_overflow_scroll_div');
        overflowScrollElement[0].scrollTop = overflowScrollElement[0].scrollHeight;
    
      }
    ]);
    

    Just because the wrapping in jQuery elements versus HTML DOM elements gets a little confusing with angular.

    Also for a chat application, I found making this assignment after your chats were loaded to be useful, you also might need to slap on short timeout as well.

    0 讨论(0)
  • 2020-11-21 12:03

    I have encountered the same problem, but with an additional constraint: I had no control over the code that appended new elements to the scroll container. None of the examples I found here allowed me to do just that. Here is the solution I ended up with .

    It uses Mutation Observers (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) which makes it usable only on modern browsers (though polyfills exist)

    So basically the code does just that :

    var scrollContainer = document.getElementById("myId");
    
    // Define the Mutation Observer
    var observer = new MutationObserver(function(mutations) {
    
      // Compute sum of the heights of added Nodes
      var newNodesHeight = mutations.reduce(function(sum, mutation) {
          return sum + [].slice.call(mutation.addedNodes)
            .map(function (node) { return node.scrollHeight || 0; })
            .reduce(function(sum, height) {return sum + height});
      }, 0);
    
      // Scroll to bottom if it was already scrolled to bottom
      if (scrollContainer.clientHeight + scrollContainer.scrollTop + newNodesHeight + 10 >= scrollContainer.scrollHeight) {
        scrollContainer.scrollTop = scrollContainer.scrollHeight;
      }
    
    });
    
    // Observe the DOM Element
    observer.observe(scrollContainer, {childList: true});
    

    I made a fiddle to demonstrate the concept : https://jsfiddle.net/j17r4bnk/

    0 讨论(0)
  • 2020-11-21 12:03

    Javascript or jquery:

    var scroll = document.getElementById('messages');
       scroll.scrollTop = scroll.scrollHeight;
       scroll.animate({scrollTop: scroll.scrollHeight});
    

    Css:

     .messages
     {
          height: 100%;
          overflow: auto;
      }
    
    0 讨论(0)
提交回复
热议问题