微信小程序获取文本宽度。进行显隐操作

肯定是动态数据,在拿到数据之后,先给数据一个控制显隐的值。

js 复制代码
            const query = wx.createSelectorQuery();
            let containerWidth;
            // 大容器宽高永远都不会动,所以只取一个
            query.select(".no_info_cause").boundingClientRect(containerRect=>{  // 大容器
              console.log('containerRect.width', containerRect.width);
              containerWidth = containerRect.width;
            });
            // 动态拿的值根据内容撑开宽高,所以需要动态拿取所有的。
            query.selectAll("#myContainer").boundingClientRect(rects=>{ // 文字容器
              console.log("rect", rects, containerWidth);
              rects.forEach((item, index) => {
                if (item.width > containerWidth) {
                  that.data.couponListBu.forEach((_, i) => {
                    that.data.couponListBu[index].isShow = true;
                  })
                  console.log('文本超出了容器', that.data.couponListBu[index].isShow);
                } else {
                  that.data.couponListBu.forEach((_, i) => {
                    that.data.couponListBu[index].isShow = false;
                  })
                  console.log('文本没有超出容器', that.data.couponListBu[index].isShow);
                }
              });
            }).exec();
            // 拿不到值所以用了定时器,有大神知道的可以教一下。
            setTimeout(() => {
              that.setData({
                couponListBu: that.data.couponListBu
              })
            }, 300);
html 复制代码
<view class="no_info">
<!-- 不可用原因:<text>{{item.reason}}</text> -->
  <view class="no_info_cause">
  <!-- <text>不可用原因:</text> -->
    <text class="no_info_content_overflow" id="myContainer">
      <text id="myText">
        <text class="no_info_cause_one">不可用原因:</text>{{item.reason}}
      </text>
    </text>
  </view>
  <!-- <view class="no_info_content"wx:if="{{ itemisShow }}">{{item.reason}}</view> -->
  <view class="no_info_image">
    <image wx:if="{{ item.isShow }}" src="../../images/coupon/down.png" mode="" bindtap="onClickIsShow" />
    <!-- <image wx:if="{{ item.isShow }}" src="../../images/coupon/up.png" mode="" bindtap="onClickIsShowCause" /> -->
  </view>
</view>
相关推荐
2501_915918414 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
说私域5 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
難釋懷5 小时前
第一个小程序
小程序
春哥的研究所5 小时前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
weixin_lynhgworld5 小时前
盲盒一番赏小程序技术实现方案:高并发与防作弊的平衡之道
小程序
今日热点7 小时前
小程序主体变更全攻略:流程、资料与异常处理方案
经验分享·微信·小程序·企业微信·微信公众平台·微信开放平台
鸭鸭梨吖11 小时前
微信小程序---下拉框
微信小程序·小程序
CRMEB定制开发12 小时前
CRMEB Pro版前端环境配置指南
前端·微信小程序·uni-app·商城源码·微信商城·crmeb
mon_star°14 小时前
搭建一款结合传统黄历功能的日历小程序
微信·微信小程序·小程序·微信公众平台
The_era_achievs_hero14 小时前
微信小程序91~100
微信小程序·小程序