Scroll to bottom of div?

前端 未结 30 3220
日久生厌
日久生厌 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);
    

提交回复
热议问题