关于mCustomScrollbar自定义滚动条异步加载数据滚动条无效的问题

有人用过这个滚动条插件没:mCustomScrollbar;
在我进行异步加载的过程中,回调函数中的滚动条update无效:

$.ajax({
        type: "post",
        url: urlStr,
        dataType: 'html',
        success: function(data) {
            $(".main_body_con").html(data);
            console.log(1);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown)
        {
            alert('访问网络失败!' + errorThrown+urlStr);
        },
        complete: function(){
            console.log($(".main_body_con").html());
            console.log(2);
            $(".main_body_con").mCustomScrollbar("update");
        }
    });

官方文档里面说使用ajax的回调函数,调用mCustomScrollbar("update"),可以重新加载滚动条,可是我添加了无效。求高手解答。

阅读 11k
8 个回答
$.ajax({
        type: "post",
        url: urlStr,
        beforeSend: function() {
                    $(".main_body_con").mCustomScrollbar("destroy"); //Destroy
                },
        dataType: 'html',
        success: function(data) {
            $(".main_body_con").html(data);
            console.log(1);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown)
        {
            alert('访问网络失败!' + errorThrown+urlStr);
        },
        complete: function(){
            console.log($(".main_body_con").html());
            console.log(2);
            $(".main_body_con").mCustomScrollbar({
                        mouseWheel:true,
                        theme:"dark-thick",
                        scrollButtons:{
                            enable:true
                        }
                    });
        }
    });

踩坑许久,终于解决了,只需要加上这个就可以了
$(".main_body_con").mCustomScrollbar({

 mouseWheel:true,
 scrollButtons:{
    enable:true
 },
 advanced:{ 
    updateOnContentResize:true
}

});

要沉了么,有知道的帮助一下我吧

新手上路,请多包涵

在你追加后,马上就调用。
$(".info-area").find("ul").append(str);
$("#sendInfoList").mCustomScrollbar("update");

新手上路,请多包涵

执行异步加载的函数之前

$("(".main_body_con").mCustomScrollbar("destroy");
执行完后
$(".main_body_con").mCustomScrollbar({
                    mouseWheel:true,
                    scrollButtons:{
                        enable:true
                    }
                });
                尽量不要把mCustomScrollbar写在$.ajax中

我也踩了一次坑,说说我的一些发现吧,当调用mCustomScrollbar之后,插件会在你的div里面生成更多的DIV,其中滚动的元素放在一个div里面,如果你想动态添加内容到滚动的内容里面,就需要把内容插入这个div,而不是之前的div。

新手上路,请多包涵

在请求ajax之前 调用:
$(".Scrollbar .barY").data("mCS", ''); //手动销毁

ajax的success方法内部成功加载了html之后调用:

  //重新初始化加载mCustomScrollbar
                    $(".Scrollbar .barY").mCustomScrollbar();
推荐问题