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>
相关推荐
愚者Pro7 小时前
Flutter Widget组件学习(专为 Uniapp 转 Flutter 定制)
vue.js·学习·flutter·uni-app
道里8 小时前
花了 5 万刀用 AI 写代码之后,这是我的全部经验
前端·人工智能
Royzst8 小时前
xml知识点
java·服务器·前端
IT_陈寒8 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
kyriewen9 小时前
推行AI写代码一年后,Code Review变成了新的加班理由
前端·ai编程·cursor
前端环境观察室9 小时前
给 Agent Browser Workflow 加一层可观测性:Trace、Snapshot 和 Review Queue
前端
柒瑞10 小时前
Superpowers结合Claude code浅实战
前端
Nian.Baikal10 小时前
从零搭建离线地图服务:Nginx + Cesium/Leaflet 实战指南
运维·前端·nginx
zithern_juejin10 小时前
new 运算符
javascript
前端毕业班10 小时前
uniapp web 灵活控制 style scoped
前端·javascript·vue.js