当前位置: 首页 > news >正文

Vue - 多行文本“展开、收起”功能

TextClamp

使用 js 实现文本展开、收起,并非纯 CSS 实现。

Props:

  • fontSize:Number,默认:14
  • lines:Number,默认:1
  • lineHeight:Number,默认:20

Feat:

  • 监听插槽的变化(文本内容的变化),自动计算文本高度
  • 展开、折叠时有 transition 效果
  • 文本内容较少时(未超过行数 lines),不会展示按钮

Code

text-clamp.vue

<template><div class="text-clamp"><div class="text" :style="{height}"><span v-if="isVisible" class="btn" @click="toggle">{{isExpand ? '收起' : '... 展开'}}</span><div ref="textRef" :style="commonStyle"><slot /></div></div></div>
</template><script>
export default {name: "TextClamp",props: {fontSize: {type: Number,default: 14},lines: {type: Number,default: 1},lineHeight: {type: Number,default: 20},selectors: {type: String,default: ""}},data () {return {isExpand: false,isVisible: false,textHeight: 0}},computed: {height () {if (this.isExpand) {return this.$refs.textRef.clientHeight + 'px';} else {return Math.min((this.lines * this.lineHeight), this.textHeight) + 'px';}},commonStyle () {return {lineHeight: this.lineHeight + 'px',fontSize: this.fontSize + 'px',}}},mounted () {this.init();// 监听插槽变化const observer = new MutationObserver((mutations) => {mutations.forEach((mutation) => {if (mutation.type === "characterData") {this.init();}});});observer.observe(this.$refs.textRef, {characterData: true,subtree: true,childList: true});},methods: {init () {this.isExpand = false;this.textHeight = this.$refs?.textRef?.clientHeight || 0;this.isVisible = this.textHeight > this.lines * this.lineHeight;},toggle () {this.isExpand = !this.isExpand;if (!this.isExpand && this.selectors) {const initEl = document.querySelector(this.selectors);setTimeout(() => {initEl.scrollIntoView({behavior: 'smooth',block: 'start',inline: 'center'});}, 97)}}}
}
</script><style lang="scss" scoped>
.text-clamp {display: flex;overflow: hidden;
}
.text {font-size: 20px;transition: 0.3s height;
}
.text::before {content: "";height: calc(100% - 20px);float: right;
}
.btn {float: right;clear: both;font-size: 12px;line-height: 14px;padding: 2px 6px;background: #1890ff;border-radius: 2px;color: #fff;cursor: pointer;
}
</style>

实例

<div style="min-height: 120px;"><text-clamp :lines="6">123123</text-clamp>
</div>
<text-clamp :lines="5" :line-height="24">{{data || "--"}}</text-clamp>

http://www.lryc.cn/news/272065.html

相关文章:

  • Linux操作系统基础(6):Linux的文件颜色
  • LeetCode 1758. 生成交替二进制字符串的最少操作数【字符串,模拟】1353
  • Spring-IOC-xml方式
  • HUAWEI华为荣耀MagicBook X 15酷睿i5-10210U处理器集显(BBR-WAH9)笔记本电脑原装出厂Windows10系统
  • React使用动态标签名称
  • Java异常篇----第二篇
  • 微服务(1)
  • 195.【2023年华为OD机试真题(C卷)】5G 网络建设(最小生成树—JavaPythonC++JS实现)
  • 2024年1月1日答案
  • 【算法】dp题单
  • Verilog视频信号图形显示 FPGA(iCE40)
  • 【LeetCode 面试经典150题】26. Remove Duplicates from Sorted Array 在有序数组中移除重复元素
  • linux系统下sql脚本的执行与导出
  • MyBatis学习一:快速入门
  • 零售业物流这个防漏水技术,居然没有翻车!
  • 主浏览器优化之路1——你现在在用的是什么浏览器?Edge?谷歌?火狐?360!?
  • gitlab请求合并分支
  • 使用Vue3开发学生管理系统模板1
  • 【cmake实战:番外】交叉编译——Linaro
  • 2024年年初Java5年实战面试题(北京)
  • 【Apache-2.0】springboot-openai-chatgpt超级AI大脑产品架构图
  • 如何在iPhone设备中查看崩溃日志
  • 对接第三方接口鉴权(Spring Boot+Aop+注解实现Api接口签名验证)
  • 微服务-理论(CAP,一致性协议)
  • CTFshow web入门web128-php特性31
  • 再见2023,你好2024(附新年烟花python实现)
  • Redis 的常用命令
  • 【模拟电路】模拟集成电路之神-NE555
  • 收集最新的 Sci-Hub 网址(本文章持续更新2024)
  • 针对NPC客户端的升级(脚本执行)