Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically scroll to bottom of a div while using Simplebar js

I am using Simplebar and angular 1 and I need to scroll to the bottom of a chatbox (div) whenever there is a new message or when you click on the menu item.

Here is my scroll function in my chatController.js

//Function to scroll the chatbox to the bottom.
function scrollToBottom(){
    var objDiv = new SimpleBar(document.getElementById('#simple'));
    objDiv.SimpleBar.getScrollElement()

    // These 2 lines of code did the trick when I didn't use Simplebar
    //var objDiv = document.getElementById("simple");
    objDiv.scrollTop = objDiv.scrollHeight;
}

And here is my html

<div id="chatbox">
    <div class="wrapper" data-simplebar id="simple">
        <div class="chatbar">
            <div>
                <p class="left">User</p>
                <p class="right">Admin</p>
            </div>
        </div>
        <div class="empty"></div>
        <div class="messages">
            <div ng-repeat="m in messages | filter: {uc_id:userCompany}:true" 
                 ng-hide="userCompany==null" >
                <div class="chat"
                     title="Sent: {{m.timestamp}}"  
                     ng-class="(m.sentByUser ? 'left' : 'right') + ' ' + 
                               (m.sentByUser && $last ? 'unread' : '')" >
                    {{m.message}}
                </div><br><br>
            </div>
        </div>
    </div>
</div>

I'm gaining this error:

Cannot set property 'SimpleBar' of null

I am also using a flexbox layout to keep the messages on the bottom (this also isn't working since simplebar. but i'm taking on 1 problem at the time)

like image 475
Martijn Vissers Avatar asked Mar 07 '17 13:03

Martijn Vissers


3 Answers

Use this

$('body').scrollTop($('body').height());

You can replace body according to your dom element

like image 138
Gaurav Kumar Singh Avatar answered Oct 23 '22 17:10

Gaurav Kumar Singh


This is the only solution that worked for me

HTML

<div id="listing-container" data-simplebar data-simplebar-auto-hide="false">
    <div>data here</div>
</div>

JavaScript

function scrollIt() {
  const scrollElement = 
  document.getElementById('listingcontainer').SimpleBar.getScrollElement();
  scrollElement.scrollTop = 1200;
}
like image 25
Malcolm Swaine Avatar answered Oct 23 '22 16:10

Malcolm Swaine


This will work

$("#chatbox .simplebar-content-wrapper").scrollTop($("#chatbox .simplebar-content-wrapper").prop("scrollHeight"));
like image 1
ramin imani Avatar answered Oct 23 '22 18:10

ramin imani