buzinas / simple-scrollbar

Very simple and lightweight vanilla javascript library for creating a custom scrollbar cross-browser.
MIT License
575 stars 159 forks source link

scrollbar not working on dynamically created divs #65

Closed WriterStat closed 3 years ago

WriterStat commented 3 years ago

Hi! I create my divs using a json dynamically, just wondering the best way to use this, thanks!

I tried: `

` `document.querySelector(".div_class").setAttribute("ss-container", ""); SimpleScrollbar.initAll();` No errors. The attribute is added. Everything loaded correctly. But the divs display the regular browser scrollbar. Also, what's the best way to use this on divs dynamically inserted into a page after page creation? Thanks! Looks great, If I can get it to work on dynamically created divs.
WriterStat commented 3 years ago

Note github ate my code text. =) But you get the gist, how do I use this for dynamically created divs?

buzinas commented 3 years ago

From README's Manual Binding:

<div class="myClass"></div>

<script>
  var el = document.querySelector('.myClass');
  SimpleScrollbar.initEl(el);
</script>
WriterStat commented 3 years ago

Will give it a try. much thx! And report back. thx!

Question...will this init all of the dynamically pre-created divs at once? Or only just the 1 element at a time.

Because:

document.querySelector('.myClass');

Will only get the first element.

Because...

I used .initAll();

Because the Read Me stated it was for using if you have multiple dynamically precreated elements to initialize all at one time.

I used:

document.querySelector(".div_class").setAttribute("ss-container", ""); SimpleScrollbar.initAll();

Because it was suggested for multiple dynamically created divs already added on the page.

So let's say I have 30 precreated dynamically created divs I want scrollbars for, best way to init them all at once? Or will I need to init one element at a time? I can, I guess. Much Thanks! And for a cool scrollbar kit!

WriterStat commented 3 years ago

...