uniapp,文字超出几行显示省略号...,展开显示更多

效果图:


代码:

复制代码
<template>
	<view class="text-container">
			<text class="text-content" @click="showDetail">{{ text }}</text>
			<text v-if="showMore" class="view-detail" @click="showDetail" style="color: #457AE6;">查看详情</text>
			<text v-if="showText&&!showMore" class="view-detail" @click="showShrink" style="color: #457AE6;">收起</text>
	</view>
</template>

<script>
	export default {
		mounted() {
			this.text = '这里是一段很长的文本内容,我们需要对超出一定长度的见到过伤筋动骨几十个地级市的世界观的几十个地级市高端就是给大家圣诞节后视镜的呼声结婚的结婚建设大街的好时机回到家';
			this.textyl = this.text
			this.checkTextLength();
		},
		data() {
			return {
				text: '',//剪切之后的文字
				textyl:'',//原来的字符
				showMore: false,//判断显示查看或者收起   小于40字不展示
				textlength: false,//判断是否点击详情 true为点击详情 false为点击收起
				showText:false//收起文字的显示隐藏
			};
		},
		computed: {
		
		},
		methods: {
			checkTextLength() {
				if (this.text.length > 40 && !this.textlength) {
					this.showMore = true;
					this.showText = false
					this.text = this.text.substring(0, 40) + '...';
				} else {
					this.showText= true
					this.showMore = false;
					this.textlength = false
					this.text = this.textyl
				}
			},
			showDetail() {
				if (this.showMore) {
					this.textlength = true
					this.checkTextLength()
				}
			},
			showShrink() {
				if (this.showText) {
					this.textlength = false
					this.checkTextLength()
				}
			},
		}
	};
</script>

<style>
	.clamp-text {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	view {
		color: #007aff;
	}
</style>
相关推荐
用户3076752811271 分钟前
💡 从"傻等"到"流淌":我在AI项目中实现流式输出的血泪史(附真实代码+深度解析)
前端
bluceli2 分钟前
前端性能优化实战指南:让你的网页飞起来
前端·性能优化
UIUV2 分钟前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
SuperEugene4 分钟前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
没想好d5 分钟前
通用管理后台组件库-9-高级表格组件
前端
阿虎儿8 分钟前
React Hook 入门指南
前端·react.js
核以解忧31 分钟前
借助VTable Skill实现10W+数据渲染
前端
WangHappy33 分钟前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序
李剑一38 分钟前
要闹哪样?又出现了一款新的格式化插件,尤雨溪力荐,速度提升了惊人的45倍!
前端·vue.js
闲云一鹤1 小时前
Git LFS 扫盲教程 - 你不会还在用 Git 管理大文件吧?
前端·git·前端工程化