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
JavaScript
x
10
10
1
//Function to scroll the chatbox to the bottom.
2
function scrollToBottom(){
3
var objDiv = new SimpleBar(document.getElementById('#simple'));
4
objDiv.SimpleBar.getScrollElement()
5
6
// These 2 lines of code did the trick when I didn't use Simplebar
7
//var objDiv = document.getElementById("simple");
8
objDiv.scrollTop = objDiv.scrollHeight;
9
}
10
And here is my html
JavaScript
1
23
23
1
<div id="chatbox">
2
<div class="wrapper" data-simplebar id="simple">
3
<div class="chatbar">
4
<div>
5
<p class="left">User</p>
6
<p class="right">Admin</p>
7
</div>
8
</div>
9
<div class="empty"></div>
10
<div class="messages">
11
<div ng-repeat="m in messages | filter: {uc_id:userCompany}:true"
12
ng-hide="userCompany==null" >
13
<div class="chat"
14
title="Sent: {{m.timestamp}}"
15
ng-class="(m.sentByUser ? 'left' : 'right') + ' ' +
16
(m.sentByUser && $last ? 'unread' : '')" >
17
{{m.message}}
18
</div><br><br>
19
</div>
20
</div>
21
</div>
22
</div>
23
I’m gaining this error:
JavaScript
1
2
1
Cannot set property 'SimpleBar' of null
2
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)
Advertisement
Answer
Use this
JavaScript
1
2
1
$('body').scrollTop($('body').height());
2
You can replace body
according to your dom element