解决微信小程序中当数据少于display-multiple-items设置的数时,会出现不显示数据的情况 ?

本文介绍了两种解决微信小程序中根据图片数量动态调整轮播图显示数量的方法。第一种方法通过判断数组长度设置`display-multiple-items`属性;第二种方法使用条件渲染,分别处理图片数量大于2和小于等于2的情况。示例代码详细展示了如何实现这一功能,确保了轮播图的正确展示。
摘要由CSDN通过智能技术生成

解决办法有两种

  • 一、通过判断其数组长度
  • 二、将两种情况分开写,大于要设置的数量时,写一种样式,小于时,再写一种样式

解决方案一:

<swiper class="swiperMain"  autoplay="false" current="0" interval="3000" duration="500" circular="true" indicator-dots="{{false}}" display-multiple-items="{{finalObj.imageInfo.length>2?3:finalObj.imageInfo.length}}">
  <swiper-item class="item" wx:for="{{finalObj.imageInfo}}" wx:for-item="items" wx:key="index">
    <image src="{{items}}"></image>
  </swiper-item>
</swiper>

解决方案二

<block wx:if="{{finalObj.imageInfo.length>2}}">
  <swiper class="swiperMain" autoplay="true" current="0" interval="2000" duration="1000" circular="true" indicator-dots="{{false}}" display-multiple-items='3'>
    <swiper-item class="item" wx:for="{{finalObj.imageInfo}}" wx:for-item="items" wx:key="index">
      <image src="{{items}}"></image>
    </swiper-item>
  </swiper>
</block>
<block wx:else>
  <view class="img_normal_box">
    <image  wx:for="{{finalObj.imageInfo}}" wx:for-item="items" wx:key="index" class="img_normal" alt="" src="{{items}}"></image>
  </view>
</block>

效果图

在这里插入图片描述
:红色框中圈起来的,是轮播图,每次切换一张图片

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值