我是靠谱客的博主 机灵高跟鞋,这篇文章主要介绍vue 折叠展示多行文本组件的实现代码,现在分享给大家,希望可以做个参考。

折叠展示多行文本组件

折叠展示多行文本组件,自动根据传入的expand判断是否需要折叠
两种模式:展开/收起展示全文本(默认)、popover展示全文本

先上代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<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: { // 文本内容 type: String, default: () => '' }, expand: { // 折叠显示行数 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) //获取行高 // 计算行高 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">


到此这篇关于vue 折叠展示多行文本组件的文章就介绍到这了,更多相关vue 折叠展示多行文本组件内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是机灵高跟鞋最近收集整理的关于vue 折叠展示多行文本组件的实现代码的全部内容,更多相关vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(147)

评论列表共有 0 条评论

立即
投稿
返回
顶部