css Swiperjs 100%宽度错误,当在网格内时

pftdvrlh  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(238)

我在网格中使用SwiperJS时遇到了一个问题。当我将Swiper Package 放入with display中时:网格时, Package 器溢出100%。我提供了一个指向我的JSFiddle示例的链接。如果你删除CSS,你会看到一切正常。但是,当我添加网格时,问题就出现了。
谢谢你。
jsfiddle

<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css"
/>

<div class="item-grid">
  <div>
    <div class="swiper mySwiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 4</div>
        <div class="swiper-slide">Slide 5</div>
        <div class="swiper-slide">Slide 6</div>
        <div class="swiper-slide">Slide 7</div>
        <div class="swiper-slide">Slide 8</div>
        <div class="swiper-slide">Slide 9</div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
  </div>
  <div></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js"></script>

<script>
  var swiper = new Swiper(".mySwiper", {
    slidesPerView: 6,
    spaceBetween: 30,
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
    },
  });
</script>

.item-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 24px;
}
ruarlubt

ruarlubt1#

您可以将包含滑动器的网格项的最小宽度设置为100%。

.item-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 24px;
}

.item-grid> :first-child {
  min-width: 100%;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css" />
<div class="item-grid">
  <div>
    <div class="swiper mySwiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
        <div class="swiper-slide">Slide 4</div>
        <div class="swiper-slide">Slide 5</div>
        <div class="swiper-slide">Slide 6</div>
        <div class="swiper-slide">Slide 7</div>
        <div class="swiper-slide">Slide 8</div>
        <div class="swiper-slide">Slide 9</div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
  </div>
  <div>content</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js"></script>
<script>
  var swiper = new Swiper(".mySwiper", {
    slidesPerView: 6,
    spaceBetween: 30,
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
    },
  });
</script>
drkbr07n

drkbr07n2#

你可以用flex代替grid。

.item-grid {
   display: flex;
}
.item-grid > div {
   width: 50%;
}

使用此样式。

相关问题