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

javascript - How to enable center slides only for middle slides in Swiper JS? - Stack Overflow

programmeradmin1浏览0评论

Working with Swiper JS for a slider and want to enable center slides only for the middle slides and not the first and last slides. Is there an easy and configurable way to specify the index of the centered slides in the following?

 var swiper = new Swiper('.my-swiper-section', {
    slidesPerView: auto,
    spaceBetween: 1
  });

I came up with the following, but it is now working 100% correctly:

 swiper.on('slideChangeTransitionStart', function () {
      if (swiper.isBeginning || swiper.isEnd) {
           swiper.params.centeredSlides = false;
      }
      else {
           swiper.params.centeredSlides = true;
      }
      swiper.update();
 });

Working with Swiper JS for a slider and want to enable center slides only for the middle slides and not the first and last slides. Is there an easy and configurable way to specify the index of the centered slides in the following?

 var swiper = new Swiper('.my-swiper-section', {
    slidesPerView: auto,
    spaceBetween: 1
  });

I came up with the following, but it is now working 100% correctly:

 swiper.on('slideChangeTransitionStart', function () {
      if (swiper.isBeginning || swiper.isEnd) {
           swiper.params.centeredSlides = false;
      }
      else {
           swiper.params.centeredSlides = true;
      }
      swiper.update();
 });
Share Improve this question edited Dec 1, 2018 at 18:22 Mark A asked Oct 30, 2018 at 2:05 Mark AMark A 611 gold badge1 silver badge5 bronze badges 3
  • Hello, did you found a solution for this? I also strugling with the same problem. – hsantos Commented Aug 21, 2019 at 11:45
  • I know this is an old thing, but I was having the same issues and manage to make it work but with ugly transitions. It would be nice to know what the author of the Swiper would propose as a solution. – lokarkristina Commented Dec 24, 2019 at 9:29
  • No didn't find any solution yet for this! – Mark A Commented Apr 19, 2020 at 18:42
Add a comment  | 

3 Answers 3

Reset to default 14

Just found the real solution!

You need to add 2 params to your Swiper.

{
   centeredSlides: true,
   centeredSlidesBounds: true
}

Hope it will help someone in the future!

I think solution is adding parameter "initialSlide". Example:

 var swiper = new Swiper('.my-swiper-section', {
    slidesPerView: auto,
    spaceBetween: 1,
    initialSlide: 3,
  });

I solved this problem

.swiper-slide {
  display: flex;
  justify-content: center
}
发布评论

评论列表(0)

  1. 暂无评论