在 Swiper 滑块中显示具有相同高度的所有图像

lee*_*mon 6 javascript css slider swiper.js

我正在使用Swiper脚本在网站上显示混合有横向和纵向照片的滑块,我想知道是否可以设置特定的最大高度,以便所有图像在尊重其纵横比的情况下具有相同的高度。我知道heightSwiper上有一个参数,但我尝试了一下,但似乎没有任何区别。另外,我尝试在类或类中的图像max-height上设置 a ,但图像要么被剪切,要么被扭曲。.swiper-wrapper.swiper-slide > img

有任何想法吗?

谢谢

var swiper = new Swiper( '.swiper-container', {
    initialSlide: 0, 
    loop: true,
    autoHeight: true,
    height: 200,
    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
    },
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
});
Run Code Online (Sandbox Code Playgroud)

小智 16

最简单的解决方案是添加height: auto到滑动滑块和height: 100%内部内容中。

const swiper = new Swiper('.swiper', {
  // Optional parameters
  direction: 'horizontal',
  slidesPerView: "auto",
   centeredSlides: true,

  // If we need pagination
  pagination: {
    el: '.swiper-pagination',
  },

  // Navigation arrows
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  // And if we need scrollbar
  scrollbar: {
    el: '.swiper-scrollbar',
  },
});
Run Code Online (Sandbox Code Playgroud)
.swiper {
  width: 600px;
  height: 100%;
}

.swiper-slide {
background-color: grey;
height: auto !important;
text-align: center;
}
.swiper-content {
height: 100% !important
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My Website</title>
    <link rel="stylesheet" href="./style.css">
    <link rel="icon" href="./favicon.ico" type="image/x-icon">
    <link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css"/>
    <script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>
  </head>
  <body>
    <main>
        <div class="swiper">
          <!-- Additional required wrapper -->
          <div class="swiper-wrapper">
            <!-- Slides -->
            <div class="swiper-slide">
              <div class="swiper-content">
                <p>Slide 1 Content</p>
                <p>Slide 1 Content</p>
                <p>Slide 1 Content</p>
                <p>Slide 1 Content</p>
              </div>
            </div>
            <div class="swiper-slide">
              <div class="swiper-content">
                <p>Slide 2 Content</p>
              </div>
            </div>
            <div class="swiper-slide medium-content">
              <div class="swiper-content">
                <p>Slide 3 Content</p>
                <p>Slide 3 Content</p>
              </div>
            </div>
            ...
          </div>
          <!-- If we need pagination -->
          <div class="swiper-pagination"></div>

          <!-- If we need navigation buttons -->
          <div class="swiper-button-prev"></div>
          <div class="swiper-button-next"></div>

          <!-- If we need scrollbar -->
          <div class="swiper-scrollbar"></div>
        </div>
    </main>
    <script src="index.js"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)