CSS文本展开收起

css实现文本展开收起  并且文本环绕按钮

 

 已封装成组件  拿走不谢!!!

<template>
	<div class="warp">
		<div class="text" :class="isOpen ? '' : 'ellipsis'" id="ellipsis_box">
			<span class="switch" v-if="!isOpen && !isShowBtn"><text style="margin-right: 10rpx;"
					@click="handleSwitch()">
					▼</text>展开</span>
			<span class="switch" v-if="isOpen && !isShowBtn"><text style="margin-right: 10rpx;" @click="handleSwitch()">
					▲
				</text>收起</span>
			<text v-html="content"></text>
		</div>
	</div>
</template>

<script>
	export default {
		props: {
			content: {
				type: String,
				required: true
			}
		},
		data() {
			return {
				isOpen: false,
				isShowBtn: true
			}
		},
		mounted() {
			const box = document.getElementById('ellipsis_box')
			if (box.scrollHeight > box.clientHeight) {
				this.isShowBtn = false
			}
		},
		methods: {
			handleSwitch() {
				this.isOpen = !this.isOpen
			},
		}
	}
</script>
<style lang="scss" scoped>
	.warp {
		display: flex;

		.ellipsis {
			display: -webkit-box;
			-webkit-line-clamp: 3;
			-webkit-box-orient: vertical;
			overflow: hidden;
		}

		.text {
			.switch {
				float: right;
				clear: both;
				color: #21a0ff;
				margin-right: 10rpx;
			}
		}

		.text::before {
			content: '';
			width: 0rpx;
			height: calc(100% - 38rpx);
			background-color: red;
			float: right;
		}
	}
</style>

温馨提示:引用此组件  传个  content即可!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值