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

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

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>
相关推荐
greenbbLV1 小时前
中小公司积分商城选型:SaaS与私有化方案对比解析
大数据·前端·小程序
2501_915909061 天前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张1 天前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
海纳百川·纳海川1 天前
租房行业数字化:换个思路解决“老问题”
大数据·微信小程序·小程序
上海云盾-小余2 天前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
2501_916008892 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
2301_810313182 天前
海外教育APP定制发展现状与开发选型核心要点解析
小程序·团队开发·软件需求
2501_915921432 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode
禾高网络2 天前
回收小程序在环保和商业领域发挥更大的作用
java·大数据·人工智能·小程序
2501_916007472 天前
iOS App分发教程 App Store、TestFlight 与 Ad Hoc 的配置与上传方法
android·ios·小程序·https·uni-app·iphone·webview