buzinas / simple-scrollbar

Very simple and lightweight vanilla javascript library for creating a custom scrollbar cross-browser.
MIT License
575 stars 159 forks source link

Scrolling to the bottom #12

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hello,

I have a DIV that load messages by default from the database. I want to know how can i make the scroll bar by default in the bottom and also scroll to the bottom for after each append to my specific DIV.

Regards,

buzinas commented 8 years ago

Supposing your div is something like:

<div id="dataFromDB" ss-container></div>

After calling the initEl method (or the initAll one), you'll have to do:

var scrollContainer = document.getElementById('dataFromDB').querySelector('.ss-content');
scrollContainer.scrollTop = scrollContainer.scrollHeight;

And everytime you append a child you can call that too. If you don't want to append it manually, you would have to use Mutation observers or something like that.

By the way, the children that you want to append to your div, should be added to the .ss-content div as well - not sure if you're doing that.

ghost commented 8 years ago

Thank you @buzinas