解决微信小程序recycle-view使用百分比单位控制宽高时出现的内容溢出问题

recycle-view是微信小程序官方推出的一个经过优化的长列表组件,但是在使用百分比单位控制高宽时有个内容溢出问题,虽然它提供了height和width的参数可以设置宽高,但每次写列表都需要去js里获取宽高并设置是较为麻烦的,所以现在来着手解决使用百分比单位设置宽度时碰到的内容溢出问题。

先看看问题怎么复现:

1.先添加组件依赖:

复制代码
{
  "usingComponents": {
    "recycle-view": "miniprogram-recycle-view/recycle-view",
    "recycle-item": "miniprogram-recycle-view/recycle-item"
  }
}

2.编写wxml:

复制代码
<view id="root" style="height: 100%;">
  <view style="height:50%;width: 100%;background-color: blue;">
    <recycle-view batch="{{batchSetRecycleData}}" id="recycleId">
      <recycle-item wx:for="{{1000}}" wx:key="id">
        <view style="width: 100px;height: 100px;">
          1234
        </view>
      </recycle-item>
    </recycle-view>
  </view>
</view>

wxss:

复制代码
page {
  width: 100%;
  height: 100%;
}

3.查看界面

4.现在来说一下问题,首先是界面显示了一个基于recycleview的列表,然后他的父元素我给他设置了height:50%,按理来说这个列表的区域也应该是Page内容区域高度的50%,也就是和蓝色区域重合,但是他现在内容溢出了

这明显是不合理的,接下来是修复

1.打开miniprogram_npm/miniprogram-recycle-view/recycle-view.wxml,编辑最外层的view的style,直接改为height:100%;width:100%

复制代码
<!--components/recycle-view/recycle-view.wxml-->
<view bindtouchstart='_beginToScroll' style="height:100%;width:100%;" id="content" class="wrap">
  <scroll-view bindscroll="_scrollViewDidScroll" class="content" style='height:100%;position: relative;' scroll-y="{{useInPage ? false : scrollY}}" scroll-x="{{false}}" upper-threshold="{{upperThreshold}}" lower-threshold="{{lowerThreshold}}" scroll-top="{{innerScrollTop}}" scroll-into-view="{{innerScrollIntoView}}" scroll-with-animation="{{scrollWithAnimation}}" bindscrolltoupper="_scrollToUpper" bindscrolltolower="_scrollToLower" scroll-anchoring enable-back-to-top="{{enableBackToTop}}" throttle="{{throttle}}">
    <view style="position: absolute;z-index:1;width:100%;left: 0;top: 0;opacity: 0;visibility: hidden;">
      <slot name="itemsize"></slot>
    </view>
    <view style="height:{{hasBeforeSlotHeight ? beforeSlotHeight + 'px' : 'auto'}}" class="slot-before">
      <slot name="before"></slot>
    </view>
    <view style='position:relative;width:100%;z-index:10;background: url("{{placeholderImageStr}}") repeat;height:{{totalHeight}}px;'>
      <!-- <view class='before' style="height:{{innerBeforeHeight}}px"></view> -->
      <view style="position: absolute;left:0;width:100%;top:{{innerBeforeHeight}}px;">
        <slot></slot>
      </view>
      <!-- <view class='after' style="height:{{innerAfterHeight}}px"></view> -->
    </view>
    <view style="height:{{hasAfterSlotHeight ? afterSlotHeight + 'px' : 'auto'}}" class="slot-after">
      <slot name="after"></slot>
    </view>
  </scroll-view>
</view>

2.打开miniprogram_npm/miniprogram-recycle-view/recycle-view.wxss,将:host{}删除或者注释

复制代码
/* components/recycle-view/recycle-view.wxss */
/* :host {
  display: block;
  width: 100%;
} */

3.运行看看效果:

内容不溢出了,而且滚动效果也正常。

微信小程序代码片段(这里只是复现该问题的例子,还需要手动安装recycleview依赖和按照上面的流程修改代码):https://developers.weixin.qq.com/s/J3TFLYmQ71K1

相关推荐
大大花猫1 小时前
我用AI写了个小程序,却被人说没有底线…
前端·微信小程序·交互设计
小小王app小程序开发1 小时前
盲盒小程序一番赏特殊玩法超细分拆解:从底层逻辑到落地细节
大数据·小程序
说私域2 小时前
基于AI大模型与AI智能名片S2B2C商城小程序的抖音内容力构建与品牌增长研究
大数据·人工智能·小程序·开源
微爱帮监所写信寄信3 小时前
微爱帮监狱写信寄信小程序DDoS防护:智能负载均衡架构
小程序·负载均衡·ddos
计算机毕设指导63 小时前
基于微信小程序的设备报修系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
qq_12498707533 小时前
悦读圈图书共享微信小程序(源码+论文+部署+安装)
spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
说私域19 小时前
移动互联网生态下定制开发开源AI智能名片S2B2C商城小程序源码在营销技术中的应用与发展
人工智能·小程序·开源
微爱帮监所写信寄信19 小时前
微爱帮监狱写信寄信工具服务器【Linux篇章】再续:TCP协议——用技术隐喻重构网络世界的底层逻辑
linux·服务器·开发语言·网络·网络协议·小程序·监狱寄信
微爱帮监所写信寄信1 天前
微爱帮监狱写信寄信小程序:MySQL核心日志与备份恢复安全架构
数据库·mysql·小程序·邮局·监狱寄信·挂号信·邮政
wan1041 天前
用户隐私协议URL
微信小程序