uniapp:展開(kāi)收起

<template>
    <view class="overflow-hidden overflow-text" :style="{ width, '-webkit-line-clamp': clamp}">
        <text class="content" :style="{ color, lineHeight: `${lineHeight}rpx`, fontSize: `${size}rpx`}">{{ content }}</text>
        <view v-if="more" :class="['btn-text', { 'btn-visible': expandStaus}]" @click.stop="onToggle">
            {{ expandStaus ? '收起' : '展開(kāi)' }}
        </view>
    </view>
</template>

<script>
    export default {
        name: "c-read-more",
        props: {
            content: {
                type: String,
                default: ''
            },
            width: {
                type: String,
                default: '100%'
            },
            lineClamp: {
                type: [String, Number],
                default: 2
            },
            lineHeight: {
                type: [String, Number],
                default: 1
            },
            size: {
                // 單位 rpx
                type: [String, Number],
                default: 26
            },
            color: {
                type: String,
                default: '#989797'
            },

        },
        data() {
            return {
                expandStaus: false,
                clamp: this.lineClamp,
                more: false
            };
        },
        mounted() {
            this.$nextTick(async () => {
                const {
                    height: cHeight
                } = await this.$uGetRect('.content')
                const {
                    height: fHeight
                } = await this.$uGetRect('.overflow-hidden')
                this.more = cHeight > fHeight
            })
        },
        methods: {
            onToggle() {
                this.expandStaus = !this.expandStaus
                if (this.expandStaus) {
                    this.clamp = 1000
                } else {
                    this.clamp = this.lineClamp
                }
                this.$emit('toggle', this.expandStaus)
            }
        }
    }
</script>

<style lang="scss" scoped>
    .overflow-hidden {
        display: -webkit-box;
        word-break: break-all;
        white-space: normal;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .overflow-text {
        position: relative;
        box-sizing: border-box;
    }

    .btn-text {
        position: absolute;
        right: 0;
        bottom: 0;
        z-index: 2;
        background-color: #fff;
        color: #FFAD2B;
        padding-left: 0.25rem;
        font-size: 26rpx;

        &::after {
            content: '';
            position: absolute;
            top: 0;
            left: -1.5rem;
            height: 100%;
            width: 1.5rem;
            background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0.5) 10%, #fff);
            pointer-events: none;
        }

        &.btn-visible {
            display: inline-block;
            position: static;
            padding-left: 0;
        }
    }
</style>
效果圖
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容