使用Swiper快速实现3D效果轮播
最近经常接到轮播图3D效果的需求, 特在此记录一下以备之后使用。
具体实现效果如下:

在这里介绍两种使用方式, 一种原生的html+php后端渲染, 一种是使用vue。
原生实现
引入
首先我们介绍原生的使用方式,按照swiper官方文档引入swiper.min.css和swiper.min.js。
html结构
html结构如下:
    
        
            {volist name="teacherInfo" id="vo"}
            
            {/volist}
        
    
css结构
css结构如下:
.jieshao{
    width: 1080px;
    height: 551px;
    margin: auto;
    overflow: hidden;
}
.swiper-slide{
    box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.05);
    border-radius: 26px;
    position: relative;
    height: 551px;
}
.swiper-img{
    position: absolute;
    width: 100%;
    height: 551px;
    border-radius: 26px;
}
.lay-pop{
    color: #fff;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    overflow: hidden;
}
.lay-pop-box{
    position: absolute;
    width: 100%;
    height: 100%;
    background: #000000;
    border-radius: 26px;
    opacity: 0.18;
}
.lay-pop .title{
    color: #fff;
    font-size: 30px;
    font-weight: bold;
    padding: 25px 30px 14px 40px;
    position: relative;
    z-index: 1;
}
.lay-pop .desc{
    width: 100%;
    height: 112px;
    font-size: 12px;
    font-weight: 400;
    padding: 0 30px 10px 40px;
    margin-bottom: 24px;
    color: #fff;
    word-break: break-all;
    display: -webkit-box;
    -webkit-line-clamp: 5;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break:break-all;
     position: relative;
    z-index: 1;
}
.swiper-slide-prev {
    transform: scale(0.9);
    opacity: 0.3;
}
.swiper-slide-next {
    transform: scale(0.9);
    opacity: 0.3;
}
.swiper-slide .lay-pop{
  display: none;
}
.swiper-slide-active .lay-pop{
  display: block;
}
js结构
js结构如下:
var mySwiper = new Swiper ('.swiper-container2', {
    autoplay: {
      delay: 3000,
      disableOnInteraction: false,
    },
    slidesPerView: 3,
    spaceBetween: 2,
    centeredSlides: true,
    centeredSlidesBounds: true,
    observer: true, //修改swiper自己或子元素时,自动初始化swiper
    observeParents: true, //修改swiper的父元素时,自动初始化swiper
    loop: true,
})        
主要通过slidesPerView设置需要显示轮播的数量,并通过swiper-slide-prev,swiper-slide-next两个类名对上一张下一张轮播进行缩小, 达到中间大而两边小的效果。
Vue实现
vue使用中还是有几个小坑的,需要大家注意一下。
安装
首先安装Swiper包npm i swiper。
引入
我目前安装的最新版Swiper是6.3.5版的, 如果是6.x的版本在这里务必要注意如果需要分页器以及自动轮播都是需要引入对应在Swiper里的组件的。
import 'swiper/swiper-bundle.css'
import Swiper, { Pagination, Navigation, Autoplay } from 'swiper';
Swiper.use([Pagination, Navigation, Autoplay]);
具体使用
swiperOptions配置:
swiperOptions: {
    pagination: {
      el: ".swiper-pagination",
    },
    autoplay: {
      delay: 3000,
      disableOnInteraction: false,
    },
    effect: "coverflow",
    slidesPerView: 1.2,
    spaceBetween: 2,
    loopedSlides: 5,
    centeredSlides: true,
    centeredSlidesBounds: true,
    observer: true, //修改swiper自己或子元素时,自动初始化swiper
    observeParents: true, //修改swiper的父元素时,自动初始化swiper
    loop: true,
},
效果
实现的效果如下:
