Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a scrollbar always bottom?

I have a scroll-bar in a div element and initially it's position is top. Whenever i add some text to the div element then the scroll-bar does not move. Is there any way, the scroll-bar of the div element's position will be always bottom and whenever I add some text to the div element, the scroll-bar also goes to the bottom automatically?

Here's my div element:

<div class='panel-Body scroll' id='messageBody'></div> 

And CSS class

.scroll {             height: 450px;             overflow: scroll;           } 

What i need is, initially the position of the scroll-bar should be bottom.

Also when i clicked a send message, I'm adding the message to the div elements 'messageBody'. On that time the the scroll-bar should be down again.

This is the current style of my scroll-bar enter image description here

And I want it to be always enter image description here

Thanks in advance.

like image 703
Shams Nahid Avatar asked Dec 01 '16 05:12

Shams Nahid


People also ask

How do you scroll to the bottom of a page automatically?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.

How do I keep my Div scrolled to the bottom?

use css position top keep it at the bottom {position : relative; bottom:0;} . Remove the css property once the user has scroll.


1 Answers

var messageBody = document.querySelector('#messageBody'); messageBody.scrollTop = messageBody.scrollHeight - messageBody.clientHeight; 
like image 61
Janet Wu Avatar answered Sep 30 '22 02:09

Janet Wu