赞
踩
要实现如上图效果,自动轮播并且选中的图片会大一些,我们可以借助插件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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。