当前位置: 首页> 财经> 创投人物 > uniapp自定义富文本现实组件(支持查看和收起)

uniapp自定义富文本现实组件(支持查看和收起)

时间:2025/8/23 20:36:07来源:https://blog.csdn.net/qq_41733851/article/details/140173756 浏览次数:0次

废话不多说上代码

CollapseText.vue

<template><view v-if="descr"><scroll-view class="collapse-text" :style="{maxHeight: computedMaxHeight}"><!-- <slot></slot> --><rich-text :nodes="descr"></rich-text></scroll-view><view class="lookcount" @click="handleViewAll">{{operateText}}<image v-if="show" src="../../static/sq@2x.png" mode=""></image><image v-else src="../../static/zk@2x.png" mode=""></image></view></view>
</template><script>export default {name: "CollapseText",props: {maxHeight: {default: 230},descr: ''},data() {return {overflow: false,show: false,operateText: '查看全部'};},computed: {computedMaxHeight() {return (!this.maxHeight || this.show || !this.overflow) ? null : this.maxHeight + 'rpx'}},mounted() {const query = uni.createSelectorQuery().in(this);query.select('.collapse-text').fields({rect: true,scrollOffset: true,size: true}, data => {this.overflow = data.height <= data.scrollHeight}).exec();},methods: {handleViewAll() {this.show = !this.showthis.operateText = this.show ? '收起' : '查看全部'}}}
</script><style>.collapse-text {overflow: hidden;}.lookcount {height: 88rpx;font-size: 24rpx;color: #646464;display: flex;align-items: center;justify-content: center;}.lookcount image {width: 32rpx;height: 32rpx;margin-left: 4rpx;}
</style>

用法

首先导入

 剩下就是直接在用template了

 最后样式补上

这里方便复制

.navcount {width: 686rpx;font-size: 28rpx;color: #646464;line-height: 40rpx;max-height: 300rpx;overflow: hidden;}

大功告成 

关键字:uniapp自定义富文本现实组件(支持查看和收起)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: