Vue3.2中使用swiper实现层叠式轮播图

介绍

vue3 中使用 swiper 实现缩略图的轮播图效果,具体如下图所示:
Vue3.2中使用swiper实现层叠式轮播图_第1张图片

代码

<template>
	<div class='index'>
		 <div class="banner-box">
	        <swiper
	        :autoplay="state.autoplay"
	        :loop="state.loop"
	        :speed="state.speed"
	        :slidesPerView="state.slidesPerView"
	        :loopedSlides="state.loopedSlides"
	        :centeredSlides="state.centeredSlides"
	        :watchSlidesProgress="state.watchSlidesProgress"
	        @setTransition="setTransition"
	        @progress="progress"
	      >
	        <swiper-slide style="width: 520px;" v-for="(item,index) in state.imgList" :key="index">
	          <img :src="item" alt="" style="width: 520px;">
	        swiper-slide>
	      swiper>
      div>
	div>
template>
<script setup>
	const state = reactive({
	  autoplay: false,
	  watchSlidesProgress: true,   //计算每个slide的progress(进度、进程)
	  loop: true,
	  slidesPerView: "auto",
	  loopedSlides: 5,   //在loop模式下使用 slidesPerview:'auto',还需使用该参数设置所要用到的loop个数。
	  centeredSlides: true,   //活动块会居中,而不是默认状态下的居左。
	  imgList: [
	    './img1.png',
	    './img2.png',
	    './img3.png',
	    './img4.png',
	    './img5.png',
	  ]
	})
	const setTransition = (swiper, transition) => {
      // 每当设置Swiper开始过渡动画时执行。
      // transtion获取到的是Swiper的speed值。
      for (var i = 0; i < swiper.slides.length; i++) {
        var slide = swiper.slides.eq(i);
        slide.transition(transition);
      }
    };
    const progress = (progress) => {
      for (var i = 0; i < progress.slides.length; i++) {
        var slide = progress.slides.eq(i);
        var slideProgress = progress.slides[i].progress;
        var modify = 1;
        if (Math.abs(slideProgress) > 1) {
          modify = (Math.abs(slideProgress) - 1) * 0.3 + 1;
        }
        var translate = slideProgress * modify * 240 + "px";
        var scale = 1 - Math.abs(slideProgress) / 5;
        var zIndex = 999 - Math.abs(Math.round(10 * slideProgress));
        slide.transform("translateX(" + translate + ") scale(" + scale + ")");
        slide.css("zIndex", zIndex);
        slide.css("opacity", 1);
        if (Math.abs(slideProgress) > 3) {
          slide.css("opacity", 0);
        }
      }
    };
script>
<style lang="less" scoped>
.banner-box {
	width: 900px;
	margin: 0 auto;
	.swiper-slide {
		width: 520px;
		img {
			width: 520px;
		}
	}
}
style>

你可能感兴趣的:(Vue3.2,Swiper,vue3,swiper)