javascript - How to scroll down to bottom child div using jquery - Stack Overflow

I have following div structure.<div id="conversation" class="list-view"><

I have following div structure.

<div id="conversation" class="list-view">
   <div class="conv">
       <div class="msg">Hi 1</div>
       <div class="msg">Hi 2</div>
       <div class="msg">Hi 3</div>
       <div class="msg">Hi 4</div>
       <div class="msg">Hi 5</div>
       <div class="msg">Hi 6</div>
       <div class="msg">Hi 7</div>
       <div class="msg">Hi 8</div>
       <div class="msg">Hi 9</div>
       <div class="msg">Hi 10</div>
   </div>
</div>

Now when i add on ajax submit button new div <div class="msg">Hi 11</div> added at the end of <div class="conv">. i want to scroll down to the bottom new added div.

i have used following but it doesn't work.

$('.conv ').animate({scrollTop: $('.conv')[0].scrollHeight}, 'slow');
$('.conv').scrollTop($('.conv')[0].scrollHeight);

How to scroll down to bottom child div. Position of parent div is fixed.

I have following div structure.

<div id="conversation" class="list-view">
   <div class="conv">
       <div class="msg">Hi 1</div>
       <div class="msg">Hi 2</div>
       <div class="msg">Hi 3</div>
       <div class="msg">Hi 4</div>
       <div class="msg">Hi 5</div>
       <div class="msg">Hi 6</div>
       <div class="msg">Hi 7</div>
       <div class="msg">Hi 8</div>
       <div class="msg">Hi 9</div>
       <div class="msg">Hi 10</div>
   </div>
</div>

Now when i add on ajax submit button new div <div class="msg">Hi 11</div> added at the end of <div class="conv">. i want to scroll down to the bottom new added div.

i have used following but it doesn't work.

$('.conv ').animate({scrollTop: $('.conv')[0].scrollHeight}, 'slow');
$('.conv').scrollTop($('.conv')[0].scrollHeight);

How to scroll down to bottom child div. Position of parent div is fixed.

Share Improve this question edited Oct 18, 2020 at 12:42 Jaydeep Mor 1,7133 gold badges22 silver badges42 bronze badges asked Jul 15, 2015 at 10:38 DharaDhara 1,5015 gold badges30 silver badges48 bronze badges 4
  • you need to add the css for the conv div – Pete Commented Jul 15, 2015 at 10:44
  • possible duplicate of Scroll to bottom of div? – C3roe Commented Jul 15, 2015 at 10:47
  • @Jazz, I meant add your current css for the conv div to the question. I was wanting to see if you had set a height on it – Pete Commented Jul 15, 2015 at 11:02
  • yes i have added .conv{ max-height:401px; overflow:auto; } . – Dhara Commented Jul 15, 2015 at 11:58
Add a ment  | 

2 Answers 2

Reset to default 3

You will need to fix the height of .conv if you want to scroll that. Else, use scroll on the body.

Demo: http://jsfiddle/GCu2D/775/

JS:

$(document).ready(function () {
    $('.conv ').animate({
        scrollTop: $('.conv .msg:last-child').position().top
    }, 'slow');
});

CSS:

.conv {
    max-height:100px; //for demo
    overflow:auto;
}

You can use the following code as:

$('.conv').animate({scrollTop: $('.conv div.msg:last').offset().top});

For the demo : http://jsfiddle/GCu2D/776/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745439813a4627779.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信