Slick carousel - force slides to have the same height

前端 未结 8 1901
刺人心
刺人心 2021-02-01 01:21

Im having trouble with the Slick carousel JS plugin with multiple slidesToShow which have different heights.

I need the Slides to have the same

相关标签:
8条回答
  • 2021-02-01 02:00

    Ok guys i found an easy solution. Just add a setPosition callback function (fires after position/size changes) which sets the height of the slides to the height of the slider (slideTrack):

    JS

    $('.slider').slick({
        autoplay: false,
        dots: false,
        infinite: false,
        arrows: false,
        slidesToShow: 2,
        slidesToScroll: 2,
      rows: 0
    })
    .on('setPosition', function (event, slick) {
        slick.$slides.css('height', slick.$slideTrack.height() + 'px');
    });
    

    Dont forget that your slides need to have full height:

    CSS

    .slide {
      height: 100%;
    }
    

    Here is a little jsFiddle to demonstrate: https://jsfiddle.net/JJaun/o29a4q45/

    0 讨论(0)
  • 2021-02-01 02:04

    Here's an SCSS-only solution if you're OK with using object-fit:

    .slick {
      .slick-track {
        display: flex;
    
        .slick-slide {
          display: flex;
          height: auto;
    
          img {
            height: 100%;
            object-fit: cover;
            object-position: center;
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题