swiperjs自动设置每个视图的填充数 [英] swiperjs to set automatically the number of sildes per view

查看:101
本文介绍了swiperjs自动设置每个视图的填充数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swiperJS 参考链接创建一个照片转盘.

I am creating a photo carousel using swiperJS ref link here.

这是刷卡器的参数:

mySwiper = Swiper('.swiper-container', {
    loop: false,
    speed: 800,
    slidesPerView: 'auto',
    grabCursor: true,
    spaceBetween: 2,
    initialSlide: 0,
    keyboardControl: false,
    resizeReInit: true,
    autoplayDisableOnInteraction: false,
    pagination: '.swiper-pagination',
    paginationClickable: false,
    nextButton: '.swiper-next',
    prevButton: '.swiper-prev'
});

我的目的是使尽可能多的幻灯片适合视图.这些照片的宽度和尺寸不同,有些是风景模式,有些是肖像.

My purpose is to fit as many slides into the view as possible. The photos are of different width and size, some are landscape mode and some are portrait.

问题:每个视图仅显示一张幻灯片.

Problem: only showing one slide per view.

目标:尽可能多地放入幻灯片.

Goal: fit in as many slides as possible into view.

有人可以帮助我实现这一目标吗?

could somebody help me to achieve this?

推荐答案

在swiper.css中,滑动幻灯片默认设置为100%宽度:

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

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

当您使用 slidesPerView:'auto'初始化swiper时,它不会将幻灯片的宽度大小显式设置为计算得出的数字(就像您将每个视图的幻灯片定义为固定值一样)数字),但它将使用现有的幻灯片宽度.如果没有任何CSS/内联样式更改,则这些宽度将为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%.

请注意,在示例中,在 slidesPerView:'auto'的swiper网站上,您可以看到他们如何显式设置他们想要在CSS中具有不同宽度的幻灯片的宽度:

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 = new Swiper('.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>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Swiper demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/css/swiper.min.css" rel="stylesheet"/>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/js/swiper.min.js"></script>
</head>
<body>
  <!-- Swiper -->
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide"><img src="https://via.placeholder.com/150x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/350x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/250x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/450x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/250x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/550x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/300x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/400x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/500x150"></div>
      <div class="swiper-slide"><img src="https://via.placeholder.com/200x150"></div>
      <div class="swiper-slide">how about</div>
      <div class="swiper-slide">fitting some text</div>
      <div class="swiper-slide">also</div>
    </div>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
  </div>


  <!-- Initialize Swiper -->
  <script>

  </script>
</body>
</html>

这篇关于swiperjs自动设置每个视图的填充数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆