最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Swiper spaceBetween doesn't work properly - Stack Overflow

programmeradmin1浏览0评论

I'am trying to use slidesPerView: 'auto' with spaceBetween: 20 property, but Swiper shows only one slide per view.

I want to show next slide right after the first one even if it will be cutted by the document width.

  $(document).ready(function () {
     //initialize swiper when document ready
     var mySwiper = new Swiper('.swiper-container', {
        // Optional parameters
        loop: false,
        slidesPerView: 3,
        spaceBetween: 40,
        navigation: {
           prevEl: '.slider .prev-btn',
           nextEl: '.slider .next-btn',
        },
        breakpoints: {
           1200: {
              slidesPerView: 'auto',
              spaceBetween: 40,
           },
           830: {
              slidesPerView: 'auto',
              spaceBetween: 10, // <- doesn't work
           }
        }
     });
  });

But it shows only one slide per view and ignores the space which is set in options.

How to set space between slides strictly?

I'am trying to use slidesPerView: 'auto' with spaceBetween: 20 property, but Swiper shows only one slide per view.

I want to show next slide right after the first one even if it will be cutted by the document width.

  $(document).ready(function () {
     //initialize swiper when document ready
     var mySwiper = new Swiper('.swiper-container', {
        // Optional parameters
        loop: false,
        slidesPerView: 3,
        spaceBetween: 40,
        navigation: {
           prevEl: '.slider .prev-btn',
           nextEl: '.slider .next-btn',
        },
        breakpoints: {
           1200: {
              slidesPerView: 'auto',
              spaceBetween: 40,
           },
           830: {
              slidesPerView: 'auto',
              spaceBetween: 10, // <- doesn't work
           }
        }
     });
  });

But it shows only one slide per view and ignores the space which is set in options.

How to set space between slides strictly?

Share Improve this question asked Jul 11, 2019 at 19:59 mr.incrediblemr.incredible 4,18510 gold badges44 silver badges80 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

I had the same problem with slidesPerView: 'auto' always showing only one slide. The solution for me was with CSS: (codepen)

.swiper-container{
  width: 100%;
}
.swiper-wrapper {
  width: 50%;
}
.swiper-slide {
  text-align: center;
  width: auto;
}
.slide-image {
  height: 400px;
  width: auto;
}
.my-gallery figure {
  margin: 0px;
}

Instead of slidesPerView: 'auto', use this

breakpoints: {
      "@0.00": {
        slidesPerView: 1,
      },
      "@0.75": {
        slidesPerView: 2,
      },
      "@1.00": {
        slidesPerView: 3,
      },
      "@1.50": {
        slidesPerView: 4,
      },

I had the same problem in Vue and Tailwind.

I solved it by adding slidesPerView:'auto' property on the wrapper and CSS width: fit-content; on the slides.

<Swiper slides-per-view="auto" :space-between="10">
   <SwiperSlide class="w-fit">
     ...
   </SwiperSlide>
</Swiper>
发布评论

评论列表(0)

  1. 暂无评论