uni-app,一段文字实现展开、收起全文点点点

效果:

 

思路:

1.根据文本显示的布局中,每行大致能显示的文字个数。(实例是大致每行26个文字)

2.首先加载页面时,根据文字总长度判断是否超出自定义行数,来处理相应的数据,多余自定义行数,截取对应的文字个数做显示

<template>
	<view>
	<text class="yd-content">{{tempContent}}</text>
            <template v-if="txt_content !== null && txt_content.length > 52">
                <text class="yd-quanwen" v-if="isShowAllContent" 
                @click="toggleDescription">全文</text>
                <text class="yd-quanwen" v-else @click="toggleDescription">收起</text>
            </template>
	</view>
</template>
<script>
	export default {
		data() {
			return {
                tempContent:'',
				isShowAllContent:false,
				txt_content:"近年来极端天气频发,内容内容内容内容内容内容内容内容内容内容...."
            }
        },
        onLoad() {
            var _this = this
            var txtCntIndex = _this.txt_content.length
            if (txtCntIndex > 52) {
                _this.isShowAllContent = true
                _this.tempContent = _this.txt_content.substr(0, 51) + "..."
            } else {
                _this.isShowAllContent = false
                _this.tempContent = _this.txt_content
            }
        },
        methods:{
            // 全文与收起
            toggleDescription: function() {
                var _this = this
                if (_this.isShowAllContent) {
                    _this.isShowAllContent = false
                    _this.tempContent = _this.txt_content
                } else {
                    _this.isShowAllContent = true
                    _this.tempContent = _this.txt_content.substring(0, 51) + "..."
                }
            },
        }
    }
</script>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值