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

废话不多说上代码

CollapseText.vue

javascript 复制代码
<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.show
				this.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了

最后样式补上

这里方便复制

javascript 复制代码
.navcount {
		width: 686rpx;
		font-size: 28rpx;
		color: #646464;
		line-height: 40rpx;
		max-height: 300rpx;
		overflow: hidden;

	}

大功告成

相关推荐
闲云一鹤6 分钟前
Cesium 使用 Turf 实现坐标点移动(偏移)
前端·gis·cesium
Thomas游戏开发11 分钟前
Unity3D IL2CPP如何调用Burst
前端·后端·架构
想学后端的前端工程师21 分钟前
【微前端架构实战指南:从原理到落地】
前端·架构·状态模式
用户68026590511932 分钟前
如何利用 Endpoint Central 提高企业终端管理效率
javascript·后端·面试
Keya35 分钟前
DevEco Studio 使用技巧全面解析
前端·前端框架·harmonyos
_Rookie._36 分钟前
web请求 错误拦截
前端
青鸟北大也是北大43 分钟前
CSS单位与字体样式全解析
前端·css·html
咖啡の猫1 小时前
TypeScript 开发环境搭建
前端·javascript·typescript
co松柏1 小时前
AI+Excalidraw,用自然语言画手绘风格技术图
前端·人工智能·后端
用户81274828151201 小时前
安卓Settings值原理源码剖析存储最大的字符数量是多少?
前端