行业知识
微信小程序获取文本宽度。进行显隐操作
2026-11-30 11:08:00
来源:互联网转载
肯定���动态数据,在拿到数据之后,先给数据一个控制显隐的值。
() 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); {item.reason}} --> 不可用原因:{{item.reason}} { itemisShow }}">{{item.reason}} -->
上一篇:电脑安装战士3系统教程