PiotrS PiotrS - 13 days ago 6
CSS Question

Dynamically scroll bar at the bottom in div



$('textarea').on('input', function() {
var scrollHeight = parseInt($(this).prop('scrollHeight'));
$(this).height(scrollHeight);
$(this).prev('.Content').outerHeight(300 - $(this).outerHeight());


});

$(".Content").scrollTop($('.Content').height())

.OuterDiv
{
width:200px;
height:300px;
border:1px solid #000;
position:relative;
}
.Content
{
width:200px;
max-height:250px;
background-color:grey;
overflow:auto;
}
.text
{
resize:none;
position:absolute;
bottom:0;
left:0;
width:194px;
height:45px;
max-height:145px;
background-color:rgba(250, 120, 30,1);
font-size:16px;
font-family:Arial;
overflow:auto;
word-wrap:break-word;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="OuterDiv">
<div class="Content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<textarea class="text" placeholder="Write some text..."></textarea>
</div>





Good morning,

Have you any idea how make scroll bar still at the bottom when I writing text in text area (text area is increasing to up when add text).
After refresh page scroll bar is in the bottom but not when adding text

Answer

I found the answer on this SO post. Every time you type, you want to scroll to the bottom again to make sure you are at the bottom of the .Content.

This may be annoying for some users that purposely scrolled up within the content, so you should add an extra check for that.

$('textarea').on('input', function() {
  var scrollHeight = parseInt($(this).prop('scrollHeight'));
  $(this).height(scrollHeight);
  $(this).prev('.Content').outerHeight(300 - $(this).outerHeight());
  $('.Content').scrollTop($('.Content')[0].scrollHeight);
  
});

$('.Content').scrollTop($('.Content')[0].scrollHeight);
.OuterDiv
{
  width:200px;
  height:300px;
  border:1px solid #000;
  position:relative;
  }
.Content
{
  width:200px;
  max-height:250px;
  background-color:grey;
  overflow:auto;
  }
.text
{
  resize:none;
  position:absolute;
  bottom:0;
  left:0;
  width:194px;
  height:45px;
  max-height:145px;
  background-color:rgba(250, 120, 30,1);
  font-size:16px;
  font-family:Arial;
  overflow:auto;
  word-wrap:break-word;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="OuterDiv">
  <div class="Content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
  <textarea class="text" placeholder="Write some text..."></textarea>
</div>