uniapp小程序判断文本是否有溢出

3 篇文章 0 订阅
2 篇文章 0 订阅
该文章介绍了一种在小程序中处理文本溢出的方法,使用CSS样式隐藏超出部分并显示省略号。当文本内容超过设定的宽度时,会显示自定义样式的tooltip气泡。同时,代码示例展示了如何监听和调整以适应手机屏幕宽度变化。
摘要由CSDN通过智能技术生成

最近开发小程序遇到需要通过文本长度控制tooltip展示
css设置文字在元素中不够宽度显示为… 并且同时可以展示气泡(前提是气泡是自定义的样式),文字够宽度,则不显示气泡。并且跟随手机屏幕宽度变化。
小程序如何监听和实现呢?

效果图展示
在这里插入图片描述
解决办法:

  1. 溢出隐藏css样式
.hide {
	word-break: break-word; //换行模式
	overflow: hidden;
	text-overflow: ellipsis; //修剪文字,超过2行显示省略号
	display: -webkit-box;
	-webkit-line-clamp: 2; //此处为上限行数
	-webkit-box-orient: vertical;
}
  1. 获取到文本离页面顶部的距离
let query = wx.
createSelectorQuery()
	.in(this);
query
	.select('.toll-station-name-en').boundingClientRect(data => {
		console.log("节点离页面顶部的距离为" + data.height);
		if (data.height > 40) {//控制你文本展示的最大高度
			this.enOverflow = true
		}
	}).exec();

完整代码

<template>
	<view class="detail-content">
		<view class="block"></view>
		<view class="content">
			<view class="park-lot">
				<view class="toll-station" style="margin-right: 30rpx;">
					<view class="toll-station-name toll-station-name-en" :class="{hide:enOverflow}"
						:style="{height:(exOverflow&&!enOverflow?'40px':'auto')}" @click.stop="showEn()"
						ref="enStation">{{detail.enStation}}</view>
					<view class="tooltip" v-if="showToolTipEn">
						{{detail.enStation}}
					</view>
				</view>
				<image src="../../static/to.png" mode="" class="to"></image>
				<view class="toll-station" style="margin-left: 30rpx;">
					<view class="toll-station-name toll-station-name-ex" :class="{hide:exOverflow}"
						:style="{height:(enOverflow&&!exOverflow?'40px':'')}" @click.stop="showEx()">
						{{detail.exStation}}
					</view>
					<view class="tooltip" v-if="showToolTipEx">
						{{detail.exStation}}
					</view>
				</view>
			</view>


		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detail: {
					enStation: '我不隐藏',
					exStation: '我隐藏啦我隐藏啦我隐藏啦我隐藏啦我隐藏啦我隐藏啦'
				},
				showToolTipEn: false,
				showToolTipEx: false,
				enOverflow: false, //是否溢出
				exOverflow: false, //是否溢出
			}
		},
		onLoad() {
			this.judgmentOverflow()
		},
		methods: {
			judgmentOverflow() {
				let query = wx.
				createSelectorQuery()
					.in(this);
				query
					.select('.toll-station-name-en').boundingClientRect(data => {
						console.log("节点离页面顶部的距离为" + data.height);
						if (data.height > 40) {
							this.enOverflow = true
						}
					}).exec();
				query
					.select('.toll-station-name-ex').boundingClientRect(data => {
						console.log("节点离页面顶部的距离为" + data.height);
						if (data.height > 40) {
							this.exOverflow = true
						}
					}).exec();
			},
			showEn() {
				if (!this.enOverflow) {
					return
				}
				this.showToolTipEn = !this.showToolTipEn
			},
			showEx() {
				if (!this.exOverflow) {
					return
				}
				this.showToolTipEx = !this.showToolTipEx
			},
		}
	}
</script>

<style lang="scss" scoped>
	.detail-content {
		.content {
			width: calc(100% - 60rpx);
			margin: 0 30rpx 0;
			background-color: #fff;
			border-radius: 10rpx;
			height: 1018rpx;


			.park-lot {
				display: flex;
				padding-top: 60rpx;
				margin: 0 30rpx;

				.toll-station {
					width: 40%;
					text-align: center;
					position: relative;

					.toll-station-name {
						min-height: 39rpx;
						// max-height: 78rpx;
						// height: auto;
						font-size: 32rpx;
						font-weight: 500;
						color: #333333;
						line-height: 20px;
						
					}

					.hide {
						word-break: break-word; //换行模式
						overflow: hidden;
						text-overflow: ellipsis; //修剪文字,超过2行显示省略号
						display: -webkit-box;
						-webkit-line-clamp: 2; //此处为上限行数
						-webkit-box-orient: vertical;
					}



					//气泡提示框
					.tooltip {
						width: 120px;
						height: auto;
						background: #000000;
						opacity: 0.69;
						padding: 8px;
						position: relative;
						font-size: 13px;
						border-radius: 6px;
						font-weight: 400;
						color: #FFFFFF;
						line-height: 15px;
						position: absolute;
						top: 95rpx;
						left: 0;
						z-index: 999;
					}

					.tooltip::before {
						content: '';
						width: 0;
						height: 0;
						border-width: 8px;
						border-color: transparent transparent #000 transparent;
						border-style: dashed dashed solid dashed;
						position: absolute;
						top: -16px;
						left: 45%;
					}


				}

				.to {
					width: 67rpx;
					height: 18rpx;
					margin-top: 26rpx;
					background-size: 100%;
				}
			}


		}

	}
</style>

有用关注下呗~😍

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值