vue 折疊展示多行文本組件的實(shí)現(xiàn)代碼
折疊展示多行文本組件
折疊展示多行文本組件,自動(dòng)根據(jù)傳入的expand判斷是否需要折疊
兩種模式:展開/收起展示全文本(默認(rèn))、popover展示全文本
先上代碼
<template>
<div class="text-expand" ref="textExpand">
<div v-if="!(showPopover && showPopoverJudge)">
<span class="text-expand-content" :style="expandStyle">
{{ (text === null || text === undefined || text === '') ? '--' : text }}
</span>
<div class="expander">
<span
v-if="showBtn && showBtnJudge"
>
<span
v-if="!showFull"
class="action action-expand"
@click.stop="showFullFn(true)"
>
展開
<i v-if="showBtnIcon" class="iconfont iconxiajiantou" />
</span>
<span
v-else
class="action action-pack"
@click.stop="showFullFn(false)"
>
收起
<i v-if="showBtnIcon" class="iconfont iconshangjiantou" />
</span>
</span>
</div>
</div>
<el-popover
v-else
:placement="popoverPlace"
trigger="hover">
<div class="popover-content">
{{ text }}
</div>
<span class="text-expand-content" :style="expandStyle" slot="reference">{{ text }}</span>
</el-popover>
</div>
</template>
<script>
export default {
name: "TextExpand",
props: {
text: { // 文本內(nèi)容
type: String,
default: () => ''
},
expand: { // 折疊顯示行數(shù)
type: Number,
default: () => 3
},
showBtn: { // 展開、折疊按鈕
type: Boolean,
default: true
},
showBtnIcon: { // 展開、折疊icon
type: Boolean,
default: true
},
showPopover: { // popover顯示全文本
type: Boolean,
default: false
},
popoverPlace: { // popover位置
type: String,
default: 'bottom'
}
},
data () {
return {
showFull: false, // 是否展示全文本
expandStyle: '',
showBtnJudge: false, // 判斷是否需要折疊展示按鈕
showPopoverJudge: false // 判斷是否需要折疊展示popover
}
},
watch: {
text: function (val) {
this.judgeExpand()
}
},
mounted () {
this.judgeExpand()
},
methods: {
showFullFn (value) {
this.expandStyle = value ? '' : `display: -webkit-box;word-break: break-all;-webkit-line-clamp: ${this.expand};-webkit-box-orient: vertical;text-overflow: ellipsis;overflow: hidden;`
this.showFull = value
},
judgeExpand () { // 判斷是否需要折疊
this.$nextTick(() => {
const { expand } = this;
const textExpandStyle = window.getComputedStyle(this.$refs.textExpand)
const textExpandHeight = parseFloat(textExpandStyle.height) //獲取總高度
const textExpandLineHeight = parseFloat(textExpandStyle.lineHeight) //獲取行高
// 計(jì)算行高
const rects = Math.ceil(textExpandHeight / textExpandLineHeight)
if (rects <= expand) { // 不需要折疊展示
this.showBtnJudge = false
this.showPopoverJudge = false
} else {
this.showBtnJudge = true
this.showPopoverJudge = true
this.expandStyle = `display: -webkit-box;word-break: break-all;-webkit-line-clamp: ${this.expand};-webkit-box-orient: vertical;text-overflow: ellipsis;overflow: hidden;`
}
})
}
}
}
</script>
<style lang="less" scoped>
.text-expand{
&-content{
word-break: break-all;
white-space: pre-wrap;
}
.expander {
text-align: left;
margin-top: 6px;
.action {
display: inline-block;
font-size: 14px;
color: #0281F0;
cursor: pointer;
i {
display: inline;
font-size: 12px;
}
}
.action.action-pack {
margin-left: 0;
}
}
}
.popover-content{
max-width: 40vw;
max-height: 30vh;
overflow: hidden;
word-break: break-all;
overflow-y: auto;
}
</style>
用法
<text-expand :text="text" :expand="2" />


<text-expand :text="text" :expand="2" :showBtnIcon="false">


<text-expand :text="text" :expand="2" :showPopover="true">


到此這篇關(guān)于vue 折疊展示多行文本組件的文章就介紹到這了,更多相關(guān)vue 折疊展示多行文本組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js中用v-bind綁定class的注意事項(xiàng)
關(guān)于數(shù)據(jù)綁定一個(gè)常見需求就是操作元素的class列表和它的內(nèi)聯(lián)樣式。因?yàn)樗鼈兌际菍傩?,我們可以?v-bind 處理它們,但是使用v-bind綁定class的時(shí)候我們要有一些注意事項(xiàng),下面這篇文章就給大家分享了下要注意的方面,希望能對(duì)大家有所幫助,下面來一起看看吧。2016-12-12
vue?element表格某一列內(nèi)容過多,超出省略號(hào)顯示的實(shí)現(xiàn)
這篇文章主要介紹了vue?element表格某一列內(nèi)容過多,超出省略號(hào)顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果
這篇文章主要介紹了vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue + echarts實(shí)現(xiàn)中國省份地圖點(diǎn)擊聯(lián)動(dòng)
這篇文章主要介紹了vue + echarts實(shí)現(xiàn)中國地圖省份點(diǎn)擊聯(lián)動(dòng),需要的朋友可以參考下2022-04-04

