uni-app,文本实现展开、收起全文

导读:本篇文章讲解 uni-app,文本实现展开、收起全文,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

效果:uni-app,文本实现展开、收起全文uni-app,文本实现展开、收起全文

 思路:

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

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

代码与讲解:

1.设置参数(script中)

uni-app,文本实现展开、收起全文

 isShowAllContent:是都显示“全文”文字

txt_content:所有文字内容

tempContent:处理后显示的文字内容

2.进入页面处理数据

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
            }
        },

uni-app,文本实现展开、收起全文

 3.布局

<!– 文章收起与全文 –>
            <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>

uni-app,文本实现展开、收起全文

 4.“全文”或“收起”按钮的点击事件处理

// 全文与收起
            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) + “…”
                }
            },

uni-app,文本实现展开、收起全文

整体代码:

uni-app,文本实现展开、收起全文

完成,实现文本的展开与收起功能!!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119130.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!