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


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

    先看看问题怎么复现:

    1.先添加组件依赖:

    1. {
    2. "usingComponents": {
    3. "recycle-view": "miniprogram-recycle-view/recycle-view",
    4. "recycle-item": "miniprogram-recycle-view/recycle-item"
    5. }
    6. }

    2.编写wxml:

    1. <view id="root" style="height: 100%;">
    2. <view style="height:50%;width: 100%;background-color: blue;">
    3. <recycle-view batch="{{batchSetRecycleData}}" id="recycleId">
    4. <recycle-item wx:for="{{1000}}" wx:key="id">
    5. <view style="width: 100px;height: 100px;">
    6. 1234
    7. </view>
    8. </recycle-item>
    9. </recycle-view>
    10. </view>
    11. </view>

    wxss:

    1. page {
    2. width: 100%;
    3. height: 100%;
    4. }

    3.查看界面

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

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

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

    1. <view bindtouchstart='_beginToScroll' style="height:100%;width:100%;" id="content" class="wrap">
    2. <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}}">
    3. <view style="position: absolute;z-index:1;width:100%;left: 0;top: 0;opacity: 0;visibility: hidden;">
    4. <slot name="itemsize">slot>
    5. view>
    6. <view style="height:{{hasBeforeSlotHeight ? beforeSlotHeight + 'px' : 'auto'}}" class="slot-before">
    7. <slot name="before">slot>
    8. view>
    9. <view style='position:relative;width:100%;z-index:10;background: url("{{placeholderImageStr}}") repeat;height:{{totalHeight}}px;'>
    10. <view style="position: absolute;left:0;width:100%;top:{{innerBeforeHeight}}px;">
    11. <slot>slot>
    12. view>
    13. view>
    14. <view style="height:{{hasAfterSlotHeight ? afterSlotHeight + 'px' : 'auto'}}" class="slot-after">
    15. <slot name="after">slot>
    16. view>
    17. scroll-view>
    18. view>

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

    1. /* components/recycle-view/recycle-view.wxss */
    2. /* :host {
    3. display: block;
    4. width: 100%;
    5. } */

    3.运行看看效果:

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

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

  • 相关阅读:
    【LeetCode】刷题模版/套路合集(持续更新)
    PCF8574/ PCF8574A/ PCF8574T I2C to parellal 8-bits I/O
    20个.NET/.NET Core 优秀项目框架
    易基因|病毒抗性:全基因组DNA甲基化揭示草鱼年龄相关病毒易感性的表观遗传机制
    Spark与SQL之间NB的转换_withClumn,split及SubString
    HarmonyOS 实战开发-使用canvas实现图表系列之折线图
    PostgreSQL修炼之道笔记之准备篇(四)
    【LeetCode】【剑指offer】【数组中的逆序对】
    Basic Facilities of a Virtio Device (二)
    八大排序详解
  • 原文地址:https://blog.csdn.net/qq_16009381/article/details/132677623