我使用mCustomScrollbar作为页面上的元素,有时会重新加载.这是一个消息视图,当用户点击另一个对话时,会加载此对话的消息.但是,当我想在加载它之后滚动到底部,因为最新的消息在底部,它滚动到底部但是在底部以上几个像素("一些"可以在10到200px之间随机).
以下是一个简化示例:
// code to load another conversation
$(".conversations .conversation").click(function (e) {
var $this = $(this);
$.ajax({
url: W.sprintf('/messages/%s/fetch', $this.attr("data-cid")),
dataType: 'html'
}).done(function(data) {
$('.main_pane.messages').html(data);
// a function call to set the hight of .main_list.messages goes here
$(".main_list.messages").mCustomScrollbar(W.scroll_prefs);
$(".main_list.messages").mCustomScrollbar("scrollTo", "bottom");
// also tried adding an element at bottom and scrolling to this
// and scrollTo Number.MAX_VALUE
// also tried putting the two mCustomScrollbar lines both into
// setTimeout()
});
});
Run Code Online (Sandbox Code Playgroud)
<!-- this is what gets loaded -->
#conversation
.head
-# …Run Code Online (Sandbox Code Playgroud)