skip to Main Content

If you click the Collapsible Group Items #1 the slick inside of it is broken when setting the variableWidth: false. But if you set it to variableWidth: true it is working fine.

Here is the bootstrap doc https://bootstrapdocs.com/v3.3.5/docs/javascript/

See the codes below:

$(function() {
    $('.step-slider-a').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      arrows: true,
      infinite:false,
      dots: false,
      variableWidth: false,
      responsive: [
        {
          breakpoint: 992,
          settings: {
            slidesToShow:3,
          }
        },
        {
          breakpoint: 768,
          settings: {
            draggable:true,
            slidesToScroll:1,
            slidesToShow:1,
            centerMode:false,
            arrows: true,
            dots: false,
          }
        }
      ]
    });
    // $('a[data-toggle="collapse"]').on('shown.bs.collapse', function () {
    //   $('.step-slider-a').slick('setPosition');
    // })
});
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.css" />
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">


<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
        <div class="panel panel-default">
          <div class="panel-heading" role="tab" id="headingOne">
            <h4 class="panel-title">
              <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                Collapsible Group Item #1
              </a>
            </h4>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
            <div class="panel-body">
              <div class="row step-slider-a">
                <div class="col-lg-3" style="width: 200px;">
                  <div>Content 1</div>
                </div>
                <div class="col-lg-3" style="width: 200px;">
                  <div>Content 2</div>
                </div>
                <div class="col-lg-3" style="width: 200px;">
                  <div>Content 3</div>
                </div>
                <div class="col-lg-3" style="width: 200px;">
                  <div>Content 4</div>
                </div>
                <div class="col-lg-3" style="width: 200px;">
                  <div>Content 5</div>
                </div>
                <div class="col-lg-3" style="width: 200px;">
                  <div>Content 6</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading" role="tab" id="headingTwo">
            <h4 class="panel-title">
              <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                Collapsible Group Item #2
              </a>
            </h4>
          </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
            <div class="panel-body">
              Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading" role="tab" id="headingThree">
            <h4 class="panel-title">
              <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                Collapsible Group Item #3
              </a>
            </h4>
          </div>
          <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
            <div class="panel-body">
              Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js" type="application/javascript"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

2

Answers


  1. Chosen as BEST ANSWER

    I have an alternative solution for this slick carousel inside accordion. First this is not the perfect solution because there is a glitch to its slick height especially on first load or every time you refresh the page.

    I will keep this question open just in case others have better solutions than mine.

    Updates:

    • I added css to target collapsing and collapse.in classes.
    • I updated the shown.bs.collapse for my accordion.

    Below is the updated code:

    $(function() {
        $('.step-slider-a').slick({
          slidesToShow: 4,
          slidesToScroll: 1,
          arrows: true,
          infinite:false,
          dots: false,
          variableWidth: false
        });
          // Reinitialize Slick Carousel on accordion shown. You can use reinit or setPosition
        $('#accordion').on('shown.bs.collapse', function (e) {
          $(e.target).find('.step-slider-a').slick('reinit');
        });
    });
    .accordion-styles {
        & .collapsing {
          opacity: 0 !important;
        }
    
        & .collapse.in {
          opacity: 1 !important;
        }
        & .slick-prev:before, 
        & .slick-next:before {
          color:green;
        }
    }
    <!--
    https://stackoverflow.com/questions/78814776/slick-carousel-inside-accordion-is-broken-when-setting-the-variablewidth-to-fals
    -->
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.css" />
        <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    
    <div class="container">
      <div class="row">
        <div class="col-xs-12">
          <div class="panel-group accordion-styles" id="accordion" role="tablist" aria-multiselectable="true">
            <div class="panel panel-default">
              <div class="panel-heading" role="tab" id="headingOne">
                <h4 class="panel-title">
                  <a class="collapsed samplee" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                    Collapsible Group Item #1
                  </a>
                </h4>
              </div>
              <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
                <div class="panel-body">
                  <div class="row step-slider-a">
                    <div class="col-lg-3" style="width: 200px;">
                      <div>Content 1</div>
                    </div>
                    <div class="col-lg-3" style="width: 200px;">
                      <div>Content 2</div>
                    </div>
                    <div class="col-lg-3" style="width: 200px;">
                      <div>Content 3</div>
                    </div>
                    <div class="col-lg-3" style="width: 200px;">
                      <div>Content 4</div>
                    </div>
                    <div class="col-lg-3" style="width: 200px;">
                      <div>Content 5</div>
                    </div>
                    <div class="col-lg-3" style="width: 200px;">
                      <div>Content 6</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="panel panel-default">
              <div class="panel-heading" role="tab" id="headingTwo">
                <h4 class="panel-title">
                  <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                    Collapsible Group Item #2
                  </a>
                </h4>
              </div>
              <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                <div class="panel-body">
                  Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                </div>
              </div>
            </div>
            <div class="panel panel-default">
              <div class="panel-heading" role="tab" id="headingThree">
                <h4 class="panel-title">
                  <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                    Collapsible Group Item #3
                  </a>
                </h4>
              </div>
              <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
                <div class="panel-body">
                  Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js" type="application/javascript"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>


  2. To address this, you’ll need to listen to Bootstrap’s collapse events (shown.bs.collapse and hidden.bs.collapse) and force Slick Slider to update its layout when these events occur.

    Here’s how you can achieve this:

     $(function () {
                // Initialize Slick Slider
                $('.step-slider-a').slick({
                    slidesToShow: 4,
                    slidesToScroll: 1,
                    arrows: true,
                    infinite: false,
                    dots: false,
                    variableWidth: false,
                    responsive: [
                        {
                            breakpoint: 992,
                            settings: {
                                slidesToShow: 3,
                            }
                        },
                        {
                            breakpoint: 768,
                            settings: {
                                draggable: true,
                                slidesToScroll: 1,
                                slidesToShow: 1,
                                centerMode: false,
                                arrows: true,
                                dots: false,
                            }
                        }
                    ]
                });
    
                // Recalculate Slick Slider layout when collapse is shown
                $('#accordion').on('shown.bs.collapse', function () {
                    $('.step-slider-a').slick('setPosition');
                });
    
                // Recalculate Slick Slider layout when collapse is hidden
                $('#accordion').on('hidden.bs.collapse', function () {
                    $('.step-slider-a').slick('setPosition');
                });
    
            });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search