美文网首页
微信小程序--实现文本超出固定行数显示...和展开收起

微信小程序--实现文本超出固定行数显示...和展开收起

作者: 学无止境_cheer_up | 来源:发表于2023-07-25 11:54 被阅读0次

需求背景

当文本超过两行显示...,并且显示展开,收起

效果图

image.png

实现思路

举例简单版:循环遍历获取所有内容盒子的高度,由于行高设置的是22px,所以两行就是44px,是否显示展开/收起,只需判断小盒子高度是否超过44px;css和js配合实现

html

<view class="problem">
            <view class="title">常见问题</view>
            <view class="problem-list">
                <view class="item" v-for="(item, index) in questionList" :key="item.id">
                    <view class="item-ask">
                        <text class="text1">问</text>
                        <text class="text2">{{ item.title }}</text>
                    </view>
                    <view id="item-answer" class="item-answer" :class="{ 'visible-text': domHeightArr[index]?.showAll }"
                        >{{ item.content }}
                    </view>
                    <view class="item-more" v-if="domHeightArr[index]?.height >= 44">
                        <view class="more-box" @tap="handlerShowAll(index)">
                            <text>{{ domHeightArr[index]?.showAll ? "展开" : "收起" }}</text>
                            <image
                                class="answer-expand-icon"
                                :src="domHeightArr[index]?.showAll ? expandIcon : retractIcon"
                            />
                        </view>
                    </view>
                </view>
            </view>
        </view>

css

重点看 item-answer(内容基础样式)和 visible-text (超出显示...)分开动态显示

        .item-answer {
          font-size: 28px;
          font-weight: 400;
          color: #7b7b7b;
          line-height: 44px;
          margin: 12px 0 8px;
        }

        .visible-text {
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        } 

js

/** 答案dom元素的高度 */
    domHeightArr: DomHeightArr[] = [];

/** 展开图标 */
    expandIcon: string = "xxxxxx";

    /** 收起图标 */
    retractIcon: string = "xxxxxxx";

    /**
     * 此方法在拿到数据后调用即可
     */
    check() {
        // 判断摘要是否溢出
        Taro.nextTick(() => {
            const _this = this;
            const query = Taro.createSelectorQuery();
            query
                .selectAll(".item-answer")
                .boundingClientRect(function (rects: any) {
                    rects.forEach((rect) => {
                        _this.domHeightArr.push({
                            showAll: rect.height >= 44 ? true : false,
                            height: rect.height,
                        });
                    });
                })
                .exec();
        });
    }

    /**
     * 切换展开/收起
     * @param index 当前点击项的下标
     */
    handlerShowAll(index) {
        this.domHeightArr[index].showAll = !this.domHeightArr[index].showAll;
    }

知识扩展部分

微信小程序获取实例的几种api
api详情查看

wx.createSelectorQuery() 返回一个SelectorQuery实例
SelectorQuery :
1.select() 返回一个NodesRef 实例
2.selectAll() 返回所有匹配选择器的NodesRef 实例
3.selectViewport() 选择显示区域。可用于获取显示区域的尺寸、滚动位置等信息。
4.exec() 执行所有的请求。请求结果按请求次序构成数组,在callback的第一个参数中返回。

相关文章

网友评论

      本文标题:微信小程序--实现文本超出固定行数显示...和展开收起

      本文链接:https://www.haomeiwen.com/subject/gcqhpdtx.html