Skip to content Skip to sidebar Skip to footer

Swiperjs To Set Automatically The Number Of Sildes Per View

I am creating a photo carousel using swiperJS ref link here. Here are the params of the swiper: mySwiper = Swiper('.swiper-container', { loop: false, speed: 800, slide

Solution 1:

Swiper slides are set by default to 100% width in swiper.css:

.swiper-slide {
  ...
  width: 100%;
  height: 100%;
  ...
}

When you initialize swiper with slidesPerView: 'auto', it will not explicitly set the width sizes of the slides to a calculated number (like it would if you define the slides per view to a fixed number) but it will use the existing slide widths. Without any css/inline style changes, these widths will be 100%.

Note that in the example on the swiper website for slidesPerView: 'auto' you can see how they explicitly set the widths of the slides they want to have a different width in css:

.swiper-slide:nth-child(2n) {
      width: 40%;
    }
.swiper-slide:nth-child(3n) {
    width: 20%;
}

So if you want to use the original image widths just set the swiper slide widths to auto:

.swiper-slide {
    width: auto;
}

Code snippet to show how it works:

var swiper = newSwiper('.swiper-container', {
      slidesPerView: 'auto',
      centeredSlides: true,
      spaceBetween: 30,
      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },
    });
html, body {
  position: relative;
  height: 100%;
}
body {
  background: #eee;
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color:#000;
  margin: 0;
  padding: 0;
}
.swiper-container {
  width: 100%;
  height: 100%;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;

  /* Center slide text vertically */display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}

/* this is the important part */.swiper-container.swiper-slide {
  width: auto;
}
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><title>Swiper demo</title><metaname="viewport"content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/css/swiper.min.css" /><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/js/swiper.min.js"></script></head><body><!-- Swiper --><divclass="swiper-container"><divclass="swiper-wrapper"><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/150x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/350x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/250x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/450x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/250x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/550x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/300x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/400x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/500x150"></div><divclass="swiper-slide"><imgsrc="https://via.placeholder.com/200x150"></div><divclass="swiper-slide">how about</div><divclass="swiper-slide">fitting some text</div><divclass="swiper-slide">also</div></div><!-- Add Pagination --><divclass="swiper-pagination"></div></div><!-- Initialize Swiper --><script></script></body></html>

Post a Comment for "Swiperjs To Set Automatically The Number Of Sildes Per View"