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

【sgCollapseText】自定义组件:当内容显示不下的时候,折叠展开文本区域。

sgCollapseText

<template><!-- 展开or折叠文本 --><div :class="$options.name"><div class="text" ref="text" :expand="expand" @mouseover="judgeShowBt"><slot v-if="$slots.default" /><div v-else v-html="text" /></div><div class="bt" v-if="expand ? true : showBt"><div class="left"><slot name="btLeft" /></div><div class="right"><el-linkclass="expandBtn"type="primary":icon="`el-icon-caret-${expand ? `top` : `bottom`}`":underline="false"@click.stop="expand = !expand">{{ expand ? `折叠` : `展开` }}</el-link></div></div></div>
</template>
<script>
export default {name: `sgCollapseText`,components: {},data() {return {maxHeight: 100, //文本折叠可视区域showBt: false,expand: false,text: ``,form: {},};},props: ["data"],watch: {data: {handler(newValue, oldValue) {//console.log(`深度监听${this.$options.name}:`, newValue, oldValue);if (Object.keys(newValue || {}).length) {this.form = JSON.parse(JSON.stringify(newValue));this.$g.convertForm2ComponentParam(`text`, this);this.$g.convertForm2ComponentParam(`expand`, this);this.$g.convertForm2ComponentParam(`maxHeight`, this);this.$nextTick(() => {this.judgeShowBt();this.$el.style.setProperty(`--maxHeight`,typeof this.maxHeight === "string"? this.maxHeight: `${parseFloat(this.maxHeight)}px`); //js往css传递局部参数});}},deep: true, //深度监听immediate: true, //立即执行},expand(d) {this.$emit(`expand`, d);},},created() {},mounted() {},beforeDestroy() {},methods: {judgeShowBt() {//判断文本内容是否超出了外层DIV的高度、宽度,此时出现了纵向、横向滚动条(如果用了overflow: hidden;则不会出现滚动条)let DOM = this.$refs.text;if (DOM.scrollHeight > DOM.clientHeight) {this.showBt = true;}},},
};
</script>
<style lang="scss" scoped>
.sgCollapseText {& > .text {/*多行省略号*/overflow: hidden;max-height: var(--maxHeight);overflow-y: auto;/*上下渐变遮罩(兼容IOS)*/mask-image: linear-gradient(white calc(100% - 20px), transparent);&[expand] {max-height: revert;mask-image: revert;}}& > .bt {display: flex;justify-content: space-between;align-items: center;}
}
</style>

demo

<template><div :class="$options.name"><sgCollapseText:data="{text: `<div style='line-height: 1.6'><p>我陪你走到最后,能不能别想太多,会不会手牵着手,晚一点再到尽头,你说不该再相见只为了瞬间,谢谢你让我听见因为我在等待永远。</p><b>我一路向北,离开有你的季节,你说你好累,已无法再爱上谁。风在山路吹,过往的画面全都是不对,细数惭愧,我伤你几回。</b>我想我是太过依赖,在挂电话的刚才,坚持学单纯的小孩,<spanstyle='color: #f56c6c; font-weight: bold; font-size: 24px'>静静看守这份爱</span>,知道不能太依赖,怕你会把我宠坏,你的香味一直徘徊,我舍不得离开。缓缓飘落的枫叶像思念,为何挽回要赶在冬天来之前,爱你穿越时间,两行来自秋末的眼泪,让爱渗透了地面我要的只是你在我身边。</div>`,expand: expand,}"@expand="expand = $event"/></div>
</template>
<script>
import sgCollapseText from "@/vue/components/admin/sgCollapseText";
export default {components: { sgCollapseText },
};
</script>

相关文章:

  • 怎么用pf做网站百度指数分是什么
  • 用ps做网站导航宁波seo关键词如何优化
  • 网站开发服务器资源怎么弄黄山网络推广公司
  • 长沙网站建设政府补贴怎么学seo基础
  • 我想自己建个网站 应该怎么做网推什么意思
  • 拍拍网的网站建设公司网站制作要多少钱
  • QT6.9中opencv引用路径的其中一种设置
  • 线下ERP与电商平台API接口服务商技术对接全解析
  • Flink核心概念小结
  • GROMACS 本地部署教程:模拟生命密码,解码科学未来!
  • vue3组合API-toRefs函数
  • 算法-全排列
  • [面试精选] 0076. 最小覆盖子串
  • 品融电商:品牌全域运营的领航者,赋能中国质造新时代
  • 《Drain日志解析算法》论文阅读笔记
  • 11.11 TypedDict与Pydantic实战:Python高效状态管理秘籍
  • 从SEO到GEO:企业数字营销的进化与变革
  • 远控安全进阶之战:TeamViewer/ToDesk/向日葵设备安全策略对比
  • 深入浅出对抗学习:概念、攻击、防御与代码实践
  • 【C/C++】记录一次麻烦的Kafka+Json体验
  • 【RabbitMQ】基于Spring Boot + RabbitMQ 完成应用通信
  • 鸿蒙仓颉开发语言实战教程:自定义tabbar
  • centos7.9使用docker-compose安装kafka
  • GitAny - 無需登入的 GitHub 最新倉庫檢索工具
  • 图像分割技术的实现与比较分析
  • RabbitMQ 应用 - SpringBoot