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;

	}

大功告成

相关推荐
踩着两条虫4 分钟前
VTJ.PRO的平台介绍与特性
前端·架构·ai编程
光影少年15 分钟前
前端工程化升级
前端·javascript·react.js·前端框架
Hello--_--World22 分钟前
节流 VS 防抖 相关知识点与面试题
前端·javascript
We་ct25 分钟前
AI辅助开发术语体系深度剖析
开发语言·前端·人工智能·ai·ai编程
去伪存真32 分钟前
Superpowers 从“调教提示词”转向“构建工程规范”
前端·agent
发现一只大呆瓜38 分钟前
深度起底 Vite:从打包流程到插件钩子执行时序的全链路解析
前端·vite
jserTang42 分钟前
Claude Code 源码深度解析 - 前言
前端·javascript·后端
hehelm1 小时前
vector模拟实现
前端·javascript·算法
|晴 天|1 小时前
[特殊字符]️ Vue 3项目架构设计:从2200行单文件到24个组件
前端·javascript·vue.js
FrontAI1 小时前
深入浅出 LangChain —— 第三章:模型抽象层
前端·人工智能·typescript·langchain·ai agent