kenwheeler / slick

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

slickAdd doesn't work when responsive breakpoints are applied. #3324

Open gonssal opened 6 years ago

gonssal commented 6 years ago

slickAdd doesn't work when responsive breakpoints are applied.

====================================================================

http://jsfiddle.net/208rwLte/ You have to resize the window so the fiddle results panel has less than 500px width to see the problem.

====================================================================

Steps to reproduce the problem

  1. Enable slick with responsive breakpoints and different number of slidesToShow in the breakpoints than the default one.
  2. Use the slickAdd method to add new slides.

====================================================================

What is the expected behaviour?

You can add new slides even when a breakpoint is matched and/or they don't disappear when the window is resized and a breakpoint is matched.

====================================================================

What is observed behaviour?

When a breakpoint is matched, the slides aren't added, and if they were previously added when no breakpoints were matched, they disappear when resizing so a breakpoint is matched.

====================================================================

More Details

As a footnote, I'd like to say that I thought this would've for sure happened to someone else, but I both googled and searched in the issues for it and couldn't find anything, so sorry if it was already reported.

Prashanth05 commented 6 years ago

This worked for me by adding accessibility: false in slick options.

mktcode commented 6 years ago

Same issue here.... accessibility: false does not fix anything for me. @Prashanth05 I needed to go back to version 1.7.1 for it to work.

sebfie commented 6 years ago

Same issue

rathorevaibhav commented 5 years ago

Any luck with it? I'm experiencing the same issue. My slick options are:

{
    prevArrow: 'prev-arrow.png',
    nextArrow: 'next-arrow.png',
    dots: true,
    infinite: false,
    speed: 500,
    mobileFirst: true,
    adaptiveHeight: true,
    rows: 2,
    accessibility: false,
    responsive: [
        {
            breakpoint: 0,
            settings: {
                slidesToShow: 3,
                slidesToScroll: 3,
            }
        },
        {
            breakpoint: 767,
            settings: {
                slidesToShow: 6,
                slidesToScroll: 6,
            }
        },
        {
            breakpoint: 1199,
            settings: {
                slidesToShow: 7,
                slidesToScroll: 7,
            }
        }
    ]
};

It's working fine when I remove the responsive option.

Noido commented 5 years ago

Same problem here, someone find how to do ?

If i remove responsive option it work too.

I try with version 1.7.1 but problem is again here

@mktcode @Prashanth05 @sebfie @gonssal ?

Thanks

zedee commented 4 years ago

Same issue here.

@mktcode @Prashanth05 @sebfie @gonssal @noido : I've found a workaround, but first also a bit more about what I've found in order to shed some lights to fix this:

I've noticed reading on source code, when a breakpoint was achieved, internal function _.refresh(initial) is called, and I think it has something to do with component "demount" and "mount" each time _.refresh(initial) is called. Something that is related to your initial DOM structure.

In my case, first I have a server-side rendered page, with all preloaded items which I will add to slick (I initialize it on document.ready). Then when I reach some threshold, I load via AJAX a html view then append it via slickAdd

Observing what happened on DOM structure, I've noticed that all html added with slickAdd, when you get into some thresholds, is not appended on the DOM correctly. For some reason my workaround was the following:

My original HTML structure (first page load, init slick here):

<div id="my-slider">
    <div class="article-slide"><!-- some content here --></div>
    <div class="article-slide"><!-- some content here --></div>
    <div class="article-slide"><!-- some content here --></div>
</div>

My AJAX HTML string response before the workaround (string that I append via slickAdd):

    <div class="article-slide"><!-- some content here --></div>
    <div class="article-slide"><!-- some content here --></div>
    <div class="article-slide"><!-- some content here --></div>

My AJAX HTML response after (modified my view and this time, I append this. It now works correctly, no more breakpoint issues):

    <div>
        <div>
            <div class="article-slide"><!-- some content here --></div>
            <div class="article-slide"><!-- some content here --></div>
            <div class="article-slide"><!-- some content here --></div>
        </div>
    </div>

I have no idea why, haven't go deeper into slick source code, but I suspect there's something related to what is selected via DOM and parent/child html node issues.

gummiforweb commented 4 years ago

Was having same issue...

When slide is added from the init action via ajax, everything is fine. But when resize the browser, the responsive kicks in, and remove all the added sliders (but the original ones still there).

@zedee direction helps, below is what actually happened:


After some digger into the code, when responsive happens, it'll call these methods in sequence: checkResponsive() -> refresh() -> destroy() -> cleanUpRows()

Here, the function "cleanUpRows()" is where it messed up. (https://github.com/kenwheeler/slick/blob/master/slick/slick.js#L825)

As you can see, it grabs the original slides via '_.$slides.children().children();'.


Ok, now let's rewind back a little to when the slider is initialized.

When a slider is initialize, the methods sequence is: init() -> buildRows()

In thie "buildRows()" method, you can see it takes the original html mock up, and wrap with 2 divs. (https://github.com/kenwheeler/slick/blob/master/slick/slick.js#L570)

Now, let's look at when addind slide, the method "slickAdd()" just added the mockup directly, without the 2 extra divs. (https://github.com/kenwheeler/slick/blob/master/slick/slick.js#L825)

So, when the "cleanUpRows()" function is called, it's '_.$slides.children().children();' will get nothing about the new slide, but only the old ones.


quick fix to make everything works is, when adding a slide, just wrap 2 extra div on it.

$slider.slick('slickAdd', '<div class="slick-item">added</div>') change to $slider.slick('slickAdd', '<div><div><div class="slick-item">added</div></div></div>')

bgp1 commented 4 years ago

I was thrashing on this bug for several hours this morning and can confirm that once I wrapped the template contents in two additional divs the slides added as I would expect.

Noido commented 4 years ago

@bgp1 can you put sample plz

marcsaleiko commented 3 years ago

I experienced the same issue. I had an empty slick slider and wanted to append elements to it with responsive settings in place. Without responsive settings it worked fine. The workaround from @gummi-io worked for me. I just wrapped the item markup with two divs <div><div>'+content+'</div></div> and it works fine. So thanks a lot for digging into that!

jigneshrupapara17 commented 1 year ago

sive option it work t

Yes definitely right this issue is version problem.