kenwheeler / slick

the last carousel you'll ever need
kenwheeler.github.io/slick
MIT License
28.5k stars 5.88k forks source link

How to dynamically add several carousels #3285

Open caliph007 opened 6 years ago

caliph007 commented 6 years ago

I am adding carousels dynamically to a webpage and have serious troubles initializing them.

I tried: 1) dynamically adding carousel html-code with custom classes ("carousel1", "carousel2", ...) 2) adding the JS-init function for custom class to the (jquery append()) => carousel is layouted, but cannot be moved/swiped

I also tried 1) dynamically adding carousel html-code with custom classes ("carousel1", "carousel2", ...) 2) eval() the JS-init function for custom class => carousel is layouted, but cannot be moved/swiped

Is there a way to add carousels to a page dynamically and initialize them?

Example (that is non working):

dynamically add these html code blocks

<div class="slider carousel1">
    <div>your content</div>
    <div>your content</div>
    <div>your content</div>
</div>

<div class="slider carousel2">
    <div>your content</div>
    <div>your content</div>
    <div>your content</div>
</div>

.. and dynamically add these JS scripts to the <head></head>
.. or try eval() then after adding the HTML. 
=> no success

<script>
    $('.carousel1').slick({
        setting - name: setting - value
    });
</script>

<script>
    $('.carousel2').slick({
        setting - name: setting - value
    });
</script>
ahmadalfy commented 6 years ago

All you have to do is to initialize it after adding the HTML. How do you do that?