Swiper3实现异形轮播:中间大两边小

18 阅读3分钟

image.png

使用的版本:Swiper 3.4.2

原生实现

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Swiper demo</title>
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <!-- Link Swiper's CSS -->
    <link rel="stylesheet" href="./css/swiper3.min.css">

    <!-- Demo styles -->
    <style>
        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: 300px;
            margin: 20px auto;
        }

        .swiper-slide {
            text-align: center;
            font-size: 18px;
            background: #fff;
            width: 60%;
            height: 200px;
            margin-top: 12.5px;

            /* 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;

            transition: all 0.5s;
        }

        .swiper-slide-active {
            transform: scale(1.2);
            margin-top: 0;
        }
    </style>
</head>

<body>
    <!-- Swiper -->
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
            <div class="swiper-slide">Slide 4</div>
            <div class="swiper-slide">Slide 5</div>
        </div>
        <!-- Add Pagination -->
        <!-- <div class="swiper-pagination"></div> -->
    </div>

    <button id="btn1">Slide 1</button>
    <button id="btn2">Slide 2</button>
    <button id="btn3">Slide 3</button>
    <button id="btn4">Slide 4</button>
    <button id="btn5">Slide 5</button>

    <!-- Swiper JS -->
    <script src="./js/swiper3.min.js"></script>

    <!-- Initialize Swiper -->
    <script>
        var swiper = new Swiper('.swiper-container', {
            pagination: '.swiper-pagination',
            slidesPerView: 2, // 每屏展示个数 设置slider容器能够同时显示的slides数量(carousel模式)。
            initialSlide: 0, // 初始化时显示第几张
            centeredSlides: true, // 设定为true时,活动块会居中,而不是默认状态下的居左。
            spaceBetween: 30, // slide之间的距离(单位px)。
            loop: true,
            slideToClickedSlide: true, // 设置为true则点击slide会过渡到这个slide。
            // 滑动回调
            onSlideChangeEnd: function (swiper) {
                // console.log(swiper.activeIndex) //切换结束时,告诉我现在是第几个slide
                console.log(swiper.realIndex) // 真实的slide索引
            }
        })

        // 指定slider索引
        document.querySelector('#btn1').addEventListener('click', function () {
            swiper.slideTo(2, 1000, false)
        })
        document.querySelector('#btn2').addEventListener('click', function () {
            swiper.slideTo(3, 1000, false)
        })
        document.querySelector('#btn3').addEventListener('click', function () {
            swiper.slideTo(4, 1000, false)
        })
        document.querySelector('#btn4').addEventListener('click', function () {
            swiper.slideTo(0, 1000, false)
        })
        document.querySelector('#btn5').addEventListener('click', function () {
            swiper.slideTo(1, 1000, false)
        })
    </script>
</body>

</html>

Vue2

swiper中文网有使用教程 image.png

教程中有提到,Vue使用Swiper需要借助vue-awesome-swiper,不同版本的Swiper对应着不同版本的vue-awesome-swiper

  • Swiper 5-6 vue-awesome-swiper@4.1.1 (Vue2)
  • Swiper 4.x vue-awesome-swiper@3.1.3 (Vue2)
  • Swiper 3.x vue-awesome-swiper@2.6.7 (Vue2)

image.png

Vue2实现的效果图:

image.png

代码:

<!-- The ref attr used to find the swiper instance -->
<template>
  <swiper :options="swiperOption" :not-next-tick="notNextTick" ref="mySwiper">
    <!-- slides -->
    <swiper-slide>I'm Slide 1</swiper-slide>
    <swiper-slide>I'm Slide 2</swiper-slide>
    <swiper-slide>I'm Slide 3</swiper-slide>
    <swiper-slide>I'm Slide 4</swiper-slide>
    <swiper-slide>I'm Slide 5</swiper-slide>
    <swiper-slide>I'm Slide 6</swiper-slide>
    <swiper-slide>I'm Slide 7</swiper-slide>
  </swiper>
</template>

<script>
// swiper options example:
export default {
  name: "my-carrousel",
  data() {
    return {
      // NotNextTick is a component's own property, and if notNextTick is set to true, the component will not instantiate the swiper through NextTick, which means you can get the swiper object the first time (if you need to use the get swiper object to do what Things, then this property must be true)
      // notNextTick是一个组件自有属性,如果notNextTick设置为true,组件则不会通过NextTick来实例化swiper,也就意味着你可以在第一时间获取到swiper对象,假如你需要刚加载遍使用获取swiper对象来做什么事,那么这个属性一定要是true
      notNextTick: true,
      swiperOption: {
        slidesPerView: 2, // 每屏展示个数 设置slider容器能够同时显示的slides数量(carousel模式)。
        centeredSlides: true, // 设定为true时,活动块会居中,而不是默认状态下的居左。
        spaceBetween: 60, // slide之间的距离(单位px)。
        loop: true,
        // swiper的各种回调函数也可以出现在这个对象中,和swiper官方一样
        onTransitionStart(swiper) {
          console.log(swiper.realIndex);
        },
        // more Swiper configs and callbacks...
        // ...
      },
    };
  },
  // you can find current swiper instance object like this, while the notNextTick property value must be true
  // 如果你需要得到当前的swiper对象来做一些事情,你可以像下面这样定义一个方法属性来获取当前的swiper对象,同时notNextTick必须为true
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    },
  },
  mounted() {
    // you can use current swiper instance object to do something(swiper methods)
    // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
    console.log("this is current swiper instance object", this.swiper);
    this.swiper.slideTo(3, 1000, false);
  },
};
</script>

<style>
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: 300px;
  margin: 20px auto;
}

.swiper-wrapper{
  height: 200px !important;
}

.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  width: 60%;
  height: 200px;
  margin-top: 10px;

  /* 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;

  transition: all 0.5s;
}

.swiper-slide-active {
  transform: scale(1.2);
  margin-top: 0;
}
</style>

补充

之前通过margin-top来实现卡片与卡片的垂直居中,这样有个弊端:需要相互计算得到距离。 但有另一种方法,.swiper-wrapper是个flex容器,给它设置align-items: center,自然而然就能垂直居中~

image.png