jquery 使用perfect-scrollbar插件在同一页面中添加多个滚动条

y4ekin9u  于 2023-06-05  发布在  jQuery
关注(0)|答案(3)|浏览(158)

我使用了perfect-scrollbar js插件。滚动条工作正常。但是当我为另一个内容(同一页面)再次添加此滚动条时,它对第二个div不起作用。如何使用此插件在同一页面中添加多个滚动条?

JS

jQuery(document).ready(function ($) {
      "use strict";
      $('#Default').perfectScrollbar();
    });

CSS

#Default.contentHolder { position:relative; margin:0px auto; margin-top: 20px; padding:0px; width: 285px; height: 450px; overflow: hidden; border: 1px solid #CCC; }

HTML

<div id="Default" class="contentHolder">
/*content goes here. scrollbar works fine here */
</div>

 <div id="Default" class="contentHolder">
    /*another content goes here. scrollbar does not work here*/
 </div>
9bfwbjaz

9bfwbjaz1#

对于最新版本,请使用此

$('.dialogInner').each(function(){ const ps = new PerfectScrollbar($(this)[0]); });

https://github.com/utatti/perfect-scrollbar/issues/246#issuecomment-356918832

zpqajqem

zpqajqem2#

1.您的HTML无效。不能在多个元素上具有相同的ID。
1.使用类来分配插件。例如:

$('.contentHolder').perfectScrollbar();

或者你也可以使用.each循环,如果它不工作。例如:

$('.contentHolder').each(function(){
    $(this).perfectScrollbar();
});
hgqdbh6s

hgqdbh6s3#

$('.contentHolder').each((index, element) => {
    new PerfectScrollbar(element);
});

相关问题