vue2+vant+H5轮播图中间大两边小的效果

本文介绍了如何在Vue项目中使用vue-awesome-swiper插件创建自动轮播的图片展示,并实现点击图片时放大效果。步骤包括安装插件、引入组件、设置选项和添加点击事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

要实现如上图效果,自动轮播并且选中的图片会大一些,我们可以借助插件vue-awesome-swiper组件

1.下载插件:
npm install vue-awesome-swiper@3.1.3 -S
2.引入:
import ‘swiper/dist/css/swiper.css’
import { swiper, swiperSlide } from ‘vue-awesome-swiper’
3.注册使用
components: {
// 注册 vue-awesome-swiper 组件
swiper,
swiperSlide
},
4.点击图片事件
加 :data-preview=“item.preview”
在绑定的iptions里加on事件
on:{click:(e) => {
this.previewImg(e)
}
}

<template>
<!-- 轮播 -->
    <div class="container">
        <swiper :options="swiperOption">
          <swiper-slide v-for=" (item,index) in images" :key="index">
             <img :src="item" alt="" :data-preview= "item"/>
          </swiper-slide>
        </swiper>
    </div>
    <!-- 弹出层预览图片-->
    <van-popup v-model="show">
    	 <img :src="popImg " alt="" />
    </van-popup>
</template>
<script>
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'

export default {
  name: 'home-page',
  components: {
    // 注册 vue-awesome-swiper 组件
    swiper,
    swiperSlide
  },
 data(){
    return{
     show:false,
     popImg :'',
     images: [
        require('@/assets/more1.png'),
        require('@/assets/more2.png'),
        require('@/assets/more3.png'),
        require('@/assets/more4.png')
      ],
       swiperOption: {
        pagination: {
          el: '.swiper-pagination', // 与slot="pagination"处 class 一致
          clickable: true // 轮播按钮支持点击
        },
        //点击事件
         on:{click:(e) => {
		 	this.previewImg(e)
		 	}
		 }
        // 自动播放
        autoplay: {
          delay: 10000,
          disableOnInteraction: false
        },
        // 循环
        loop: true,
        spaceBetween: 0, // 轮播图左右间距
        slidesPerView: 1.5, // 一屏显示的slide个数,图片宽度
        centeredSlides: true, // 居中的slide是否标记为active,默认是最左active,这样样式即可生效
        slideToClickedSlide: true // 点击的slide会居中
      }
	 }
	},
	methods:{
		previewImg(e){
		//获取当前点击的图片
		this.popImg = e.target.getAttribute('data-preview'))
		//打开pop弹框
		this.show = true
		}
	}
}
</script>

<style lang="less" scoped>
.container{
  width:100%;
  height:160px;
  .swiper-container {
    width: 100%;
    height: 100%;
    .swiper-slide {
        width:700px;
        /* 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;
        border-radius:10px;
        transition: 300ms;
        transform: scale(0.9);//小图缩放比例
        overflow: hidden;
        img{
          width:100%;
          height:100%
        }
    }
    .swiper-slide-active,
    .swiper-slide-duplicate-active {
        width:600px;
        transform: scale(1);
    }
  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值