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

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

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>
相关推荐
咖啡八杯18 小时前
微信小程序人脸认证1.0迁移2.0
后端·微信小程序
xshirleyl18 小时前
微信小程序开发week8-慕尚花坊项目
微信小程序·小程序
admin and root21 小时前
Claude+Trae大模型 配置Chrome MCP联动Yakit自动化渗透测试
微信小程序·渗透测试·自动化·攻防演练·ai安全·claude code·ai自动化渗透测试
code_li1 天前
小程序上线需要的资质证书汇总
小程序·上线·发布·资质
hnxaoli2 天前
统信小程序(十三)循环键鼠操作程序
python·小程序
i查拉图斯特拉如是2 天前
使用workbuddy 30分钟搭建微信小程序
微信小程序·小程序
IceSugarJJ2 天前
Open-AutoGLM项目学习
语言模型·微信小程序·github
2501_916008892 天前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
android·macos·ios·小程序·uni-app·iphone·webview
咖啡の猫2 天前
小程序协同工作和发布
小程序
维双云2 天前
小程序怎么制作工具?与其盲目找开发,不如先分清自己要哪一种
小程序