Swiperjs 在网格内时出现 100% 宽度的错误。

huangapple go评论63阅读模式
英文:

Swiperjs 100% width bug when inside grid

问题

I am facing an issue while using SwiperJS inside a grid. When I place the Swiper wrapper inside a <div> with display: grid, the wrapper overflows by 100%. I have provided a link to my JSFiddle example. If you remove the CSS, you will see that everything is normal. However, when I add the grid, that's when the problem occurs.

Thank you.

jsfiddle

&lt;link
  rel=&quot;stylesheet&quot;
  href=&quot;https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css&quot;
/&gt;

&lt;div class=&quot;item-grid&quot;&gt;
  &lt;div&gt;
    &lt;div class=&quot;swiper mySwiper&quot;&gt;
      &lt;div class=&quot;swiper-wrapper&quot;&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 1&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 2&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 3&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 4&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 5&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 6&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 7&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 8&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 9&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;swiper-pagination&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
  var swiper = new Swiper(&quot;.mySwiper&quot;, {
    slidesPerView: 6,
    spaceBetween: 30,
    pagination: {
      el: &quot;.swiper-pagination&quot;,
      clickable: true,
    },
  });
&lt;/script&gt;

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

I am facing an issue while using SwiperJS inside a grid. When I place the Swiper wrapper inside a <div> with display: grid, the wrapper overflows by 100%. I have provided a link to my JSFiddle example. If you remove the CSS, you will see that everything is normal. However, when I add the grid, that's when the problem occurs.

Thank you.

jsfiddle

&lt;link
  rel=&quot;stylesheet&quot;
  href=&quot;https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.css&quot;
/&gt;

&lt;div class=&quot;item-grid&quot;&gt;
  &lt;div&gt;
    &lt;div class=&quot;swiper mySwiper&quot;&gt;
      &lt;div class=&quot;swiper-wrapper&quot;&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 1&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 2&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 3&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 4&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 5&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 6&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 7&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 8&lt;/div&gt;
        &lt;div class=&quot;swiper-slide&quot;&gt;Slide 9&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;swiper-pagination&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
  var swiper = new Swiper(&quot;.mySwiper&quot;, {
    slidesPerView: 6,
    spaceBetween: 30,
    pagination: {
      el: &quot;.swiper-pagination&quot;,
      clickable: true,
    },
  });
&lt;/script&gt;

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

答案1

得分: 0

你可以使用flex代替grid。

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

使用这个样式。

英文:

You can use flex instead of grid.

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

Use this style.

答案2

得分: 0

你可以将包含swiper的网格项的最小宽度设置为100%。

英文:

You can set the min-width of the grid item containing the swiper to 100%.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

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

.item-grid&gt; :first-child {
  min-width: 100%;
}

<!-- language: lang-html -->

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

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 05:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定