Slick carousel - force slides to have the same height

前端 未结 8 2007
刺人心
刺人心 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 01:56

    Above suggestions didn't work for me. My slider images are al portrait but might have different h/w aspect ratios. I fixed it using js. Feels ugly though, wish I found something cleaner.

    $carousel.on('setPosition', function (event, slick) {
          const $slideImages = slick.$slideTrack.find('.slick-slide-img');
          if($slideImages.first()[0].clientHeight >= $slideImages.first()[0].naturalHeight) return;
          $slideImages.height('auto');
          $slideImages.width('100%');
          const imgHeights = $slideImages.map(function(){
            return this.clientHeight;
          }).get();
          const maxHeight = Math.max.apply(null, imgHeights);
          $slideImages.height(maxHeight);
          $slideImages.width('auto');
        }
    

    I had to use the setPosition event eventhough the code only needs to be executed once, after initialising slick. The init event doesn't work because the image heights on init are way off. Same for the first one or two setPosition events - hence the if($slideImages.first()[0].clientHeight >= $slideImages.first()[0].naturalHeight) return;.

提交回复
热议问题