2

我将 mCustomScrollbar 用于页面上的一个元素,该元素有时会重新加载。这是一个消息视图,当用户单击另一个对话时,会加载此对话的消息。但是,当我想在加载后滚动到底部时,由于最新消息位于底部,它不会滚动到底部,而是滚动到底部上方的几个像素(“一些”可以随机在 10 到 200 像素之间)。

下面是一个简化的例子:

// 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()
    });
});
<!-- this is what gets loaded -->
#conversation
  .head
    -# some foo
  .conv_body
    .main_list.messages.custom-scroll.hide-scrollbar.start-bottom
      -# basically a bunch of these below
      .listelem.msg
        .left
          = image_tag(user.avatar.image(:icon), size: avatar_size(:icon))
        .right
          %span.datetime= fmt_time(msg[:datetime], :time)
        .middle
          .name= link_to(user.login, user)
          .text= msg[:text]
    #new_message_container.main_input.open.threeline
      = form_for(@message) do |f|
        -# ...

CSS:只有一些边距、填充、颜色和其他东西,没什么花哨的

4

4 回答 4

8

我有同样的问题,通过首先调用更新然后在调用 scrollTo 底部之前添加 1000 的超时来解决它

$('#commentArea .mCSB_container ').append('<span class="oneComment">'+outputText+'</span><span class="commentBreaker"></span>');
$("#commentArea").mCustomScrollbar("update");
    setTimeout(function(){
        $("#commentArea").mCustomScrollbar("scrollTo","bottom");
    },1000);
于 2014-01-08T14:24:15.410 回答
4

每次通过 ajax 加载新内容时,您似乎都会初始化插件。您应该初始化一次插件(在点击事件之外):

$(".main_list.messages").mCustomScrollbar(W.scroll_prefs);

当ajax调用完成,新内容加载完毕后,调用mCustomScrollbar update方法(根据新内容更新滚动条),然后滚动到底部:

$(".main_list.messages").mCustomScrollbar("update");
$(".main_list.messages").mCustomScrollbar("scrollTo", "bottom");
于 2013-05-24T10:31:24.443 回答
0

我用了:

var d = $('#selector');
d.mCustomScrollbar({
    setTop: d.height() + "px"
});

从 DIV 的底部开始。

于 2017-01-01T04:27:10.883 回答
0

尝试在调用自定义滚动条的容器底部添加填充。

.main_list.messages  
{ 
  padding-bottom: 10px;   
}
于 2017-06-19T23:15:05.670 回答