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

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

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>
相关推荐
CRMEB系统商城20 小时前
CRMEB标准版系统(Java)v3.1正式发布
java·spring·微信小程序·php·教育电商
m0_587383001 天前
折扣卡CPS系统源码的技术架构与实战开发指南
人工智能·小程序·数据挖掘·系统架构·需求分析
2501_915106321 天前
苹果App Store上架费用及流程全面解析
android·ios·小程序·https·uni-app·iphone·webview
海岳云舟1 天前
通过Spring Authorization Server对微信小程序应用进行授权防护
微信小程序
T01156181 天前
全栈项目实战手记|艺培场馆课时预约小程序全项目开发历程完整复盘总结
运维·服务器·小程序
lhldsg1 天前
多商户团购系统源码:技术架构选型与二次开发实战指南
java·小程序·架构
2601_949950631 天前
一套小程序,解决资料整理、在线刷题和错题复盘
人工智能·小程序·刷题·练习·小程序推荐
白远山1 天前
智慧场馆解决方案小程序系统开发实战与架构设计指南
java·开发语言·小程序·架构
阿祖zu2 天前
训练师 Agent 小程序产品上线啦
微信小程序·llm·agent
Rooting++2 天前
小程序{{ }}和vue{{ }}语法的区别
前端·vue.js·小程序