1. 程式人生 > 實用技巧 >使用Swiper快速實現3D效果輪播

使用Swiper快速實現3D效果輪播

最近經常接到輪播圖3D效果的需求, 特在此記錄一下以備之後使用。

具體實現效果如下:

在這裡介紹兩種使用方式, 一種原生的html+php後端渲染, 一種是使用vue。

原生實現

引入

首先我們介紹原生的使用方式,按照swiper官方文件引入swiper.min.cssswiper.min.js

<link href="https://cdn.bootcdn.net/ajax/libs/Swiper/5.3.1/css/swiper.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/Swiper/5.3.1/js/swiper.min.js"></script>

html結構

html結構如下:

<!--教師介紹-->
<div class="jieshao">
    <div class="swiper-container2">
        <div class="swiper-wrapper">
            {volist name="teacherInfo" id="vo"}
            <div class="swiper-slide">
                <img class="swiper-img" src="__CDN__{$vo.image}" alt="" />
                <div class="lay-pop">
                    <div class="lay-pop-box">
                    </div>
                    <div class="title">
                       {$vo.name}
                    </div>
                    <div class="desc">
                    {$vo.description}
                    </div>
                </div>
            </div>
            {/volist}
        </div>
    </div>
</div>

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-prevswiper-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]);

具體使用

<div v-if="images.length" class="banner">
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide v-for="item of images" :key="item.id">
      <img class="banner-image" :src="item.image" alt="" />
      <!-- <div class="title">{{ image }}</div> -->
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</div>

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,
},
<style lang="scss">
.home {
  .swiper-slide {
    width: 340px;
    background-color: #ebedf0;
    border-radius: 6px;
    .banner-image {
      border-radius: 6px;
    }
  }
  .swiper-slide-prev {
    transform: scale(0.9);
  }
  .swiper-slide-next {
    transform: scale(0.9);
  }

  .swiper-container {
    --swiper-theme-color: #fff;
  }
  .swiper-pagination.swiper-pagination-bullets {
    width: auto;
    right: 60px;
    text-align: right;
  }
  .swiper-pagination-bullet {
    width: 6px;
    height: 6px;
    background: #FFFFFF;
    border-radius: 4px;
  }
  .swiper-pagination-bullet-active {
    width: 20px;
    height: 6px;
    border-radius: 5px;
  }
}
</style>

效果

實現的效果如下: