Jquery 自定义滚动条(Malihu - mCustomScrollBar)不起作用

Jquery Custom ScrollBar ( Malihu - mCustomScrollBar ) doesn't work

我尝试为具有固定位置的左侧边栏实现自定义滚动条,但滚动条不起作用。谁能给我解释一下为什么不起作用?我试图 "force" 内部位置但没有出现。

看看 jsfiddle:

https://jsfiddle.net/1tpaeb4b/3/

    (function($){
        $(window).load(function(){
            $(".content").mCustomScrollbar(
            {
                alwaysShowScrollbar: 2,
                axis: "y",
                scrollbarPosition: "inside"
            });
        });
    })(jQuery);
#sidebar-left-container{
    position: fixed;
    height: auto;
    width: 25%;
    min-width:250px;
    bottom: 0;
    top:0;
    left: 0;
    background-color: #fff;
    color: #808080;
    padding: 15px;
}

#sidebar-left-container h2{
    margin-top: 0;
    font-size: 1.4em;
}

.truncate{
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

body{
  background-color: #f1f1f1;
}
  <div id="sidebar-left-container">          
   <h2><strong><span class="fa fa-comments-o"></span> Conversations</strong></h2>
     <div class="content">
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
    <div class="media messages">
        <div class="media-left">
            <a href=""><img src="/img/avatar-small.png" alt="avatar" class="img-circle pull-left message-img margin-right-15px" /></a>
        </div>
        <div class="media-body position-relative">
            <a href=""><h4 class="media-heading"><strong>Alessandro Corradini</strong></h4></a>
            <p class="message-text truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed est elit, vulputate vitae dignissim non, varius vitae purus. Praesent quis augue malesuada, malesuada est eu, cursus metus.</p>
        </div>
    </div>
</div>
</div>

你的 jsFiddle 有很多问题。

首先,您通过 HTTP 加载了 jquery 库,而 jsFiddle 不允许这样做。

其次,您附加 mCustomScrollbar 的方式有些无效。试试这个:

jQuery(function($) {
  $(".content").mCustomScrollbar({
    alwaysShowScrollbar: 2,
    axis: "y",
    scrollbarPosition: "inside"
  })
});

然后给你的 div.content 一些有限的高度,这样你就可以看到滚动条工作了。

查看更新后的 fiddle:https://jsfiddle.net/1tpaeb4b/4/