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;

	}

大功告成

相关推荐
MM_MS3 分钟前
Halcon控制语句
java·大数据·前端·数据库·人工智能·算法·视觉检测
程序员Agions14 分钟前
程序员武学修炼手册(二):进阶篇——小有所成,从能跑就行到知其所以然
前端·程序员
Van_Moonlight18 分钟前
RN for OpenHarmony 实战 TodoList 项目:任务完成进度条
javascript·开源·harmonyos
小画家~18 分钟前
第四十六: channel 高级使用
java·前端·数据库
Van_Moonlight26 分钟前
RN for OpenHarmony 实战 TodoList 项目:深色浅色主题切换
javascript·开源·harmonyos
小贵子的博客28 分钟前
Ant Design Vue <a-table>
前端·javascript·vue.js·anti-design-vue
m0_5027249529 分钟前
vue动态设置背景图片后显示异常
前端·css
天天进步201530 分钟前
【Nanobrowser源码分析4】交互篇: 从指令到动作:模拟点击、滚动与输入的底层实现
开发语言·javascript·ecmascript
console.log('npc')37 分钟前
vue2中子组件父组件的修改参数
开发语言·前端·javascript
Van_captain44 分钟前
rn_for_openharmony常用组件_Chip纸片
javascript·开源·harmonyos