uniApp通过swiper和scroll-view实现带缩略图轮播

 

 

 HTML:

<view class="peopleSwiper">
  <swiper class="bigImg" :current="current">
    <swiper-item v-for="(big,index) in imgSwiperData" :key="index">
      <image :src="big.imgUrl" mode="scaleToFill"></image>
    </swiper-item>
  </swiper>
  <scroll-view class="smallImg" scroll-x="true">
    <view v-for="(small,index) in imgSwiperData" :key="index" @click="changeBigPic(index)">
      <image :src="small.imgUrl" mode="widthFix" class="smallItem" :class="[index == current ? 'active' : '']"></image>
    </view>
  </scroll-view>
</view>

JS:

export default {
  data() {
    return {
      current: 0,//轮播图索引
      imgSwiperData:[
{id:0,imgUrl:"https://img1.baidu.com/it/u=2265759430,2688457354&fm=26&fmt=auto&gp=0.jpg"},                
{id:1,imgUrl:"https://img1.baidu.com/it/u=3429531540,1015649330&fm=26&fmt=auto&gp=0.jpg"},        
{id:2,imgUrl:"https://img0.baidu.com/it/u=2779568578,1953729642&fm=26&fmt=auto&gp=0.jpg"},        
{id:3,imgUrl:"https://img0.baidu.com/it/u=2668412444,819000202&fm=26&fmt=auto&gp=0.jpg"},
      ],
    }
  },
  methods:{
    //点击小图切换指定大图
    changeBigPic(index){
      this.current = index;
    },
  }
}

CSS:

.bigImg{width:100%;height:21rem;}
.bigImg image{width:100%;height:21rem;}
.smallImg{position:absolute;bottom:2rem;left:0;width:100%;padding:0 1rem;box-sizing:border-box;white-space:nowrap;text-align:center;}
.smallImg view{display:inline-block;width:16%;margin:0 0.5rem;}
.smallItem{width:100%;border-radius:5rem;box-sizing:border-box;}
.smallItem.active{border:0.1rem solid #ff7834;box-sizing:border-box;}

组件:

<!--带缩略图轮播图组件-->
<template>
<view class="peopleSwiper">
<swiper class="bigImg" :current="current">
<swiper-item v-for="(big,index) in imgSwiperData" :key="index">
<image :src="big.imgUrl" mode="scaleToFill"></image>
</swiper-item>
</swiper>
<scroll-view class="smallImg" scroll-x="true">
<view v-for="(small,index) in imgSwiperData" :key="index" @click="changeBigPic(index)">
<image :src="small.imgUrl" mode="widthFix" class="smallItem" :class="[index == current ? 'active' : '']"></image>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
  name:"ImgSwiper",
  props:{
    imgSwiperData:Array,
    //imgUrl 图片地址
  },
  data() {
    return {
      current: 0,//轮播图索引
    }
  },
  methods:{
    //点击小图切换指定大图
    changeBigPic(index){
      this.current = index;
    },
  }
}
</script>
<style>
.bigImg{width:100%;height:21rem;}
.bigImg image{width:100%;height:21rem;}
.smallImg{position:absolute;bottom:2rem;left:0;width:100%;padding:0 1rem;box-sizing:border-box;white-space:nowrap;text-align:center;}
.smallImg view{display:inline-block;width:16%;margin:0 0.5rem;}
.smallItem{width:100%;border-radius:5rem;box-sizing:border-box;}
.smallItem.active{border:0.1rem solid #ff7834;box-sizing:border-box;}
</style>

以下是使用swiperscroll-view实现长图左右切换和图片滚动功能的示例代码: HTML部分: ```html <swiper class="swiper-container"> <swiper-item> <scroll-view scroll-x="true" class="scroll-view"> <view class="image-wrapper"> <image src="image1.jpg" class="image"></image> </view> <view class="image-wrapper"> <image src="image2.jpg" class="image"></image> </view> <view class="image-wrapper"> <image src="image3.jpg" class="image"></image> </view> <!-- 添加更多图片 --> </scroll-view> </swiper-item> </swiper> ``` CSS部分: ```css .swiper-container { width: 100%; height: 300px; } .scroll-view { white-space: nowrap; } .image-wrapper { display: inline-block; width: 100%; height: 100%; } .image { width: 100%; height: 100%; } ``` JavaScript部分(使用了swiper的初始化代码): ```javascript import Swiper from 'swiper'; new Swiper('.swiper-container', { slidesPerView: 'auto', spaceBetween: 10, }); ``` 在这个示例中,我们首先创建一个swiper容器,并在其中嵌套一个scroll-view组件。scroll-view设置`scroll-x`属性为true,使其可以横向滚动。然后,我们在scroll-view中添加多个图片,每个图片都被包裹在一个image-wrapper中。为了保持图片的宽高比例,我们在CSS中设置了相应的样式。 最后,我们使用了swiper的初始化代码来初始化swiper容器,使其支持左右切换和图片滚动的功能。你可以根据需要自定义swiper的配置选项。 请注意,上述示例中的图片路径和样式仅供参考,你需要根据实际情况进行修改。另外,需要确保正确引入swiperscroll-view的相关资源文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值