在el-table中有提供的屬性,實現超出單元格后顯示省略號,鼠標懸浮時顯示詳細文本,那么在普通文本中怎么實現類似的功能呢?↓
1.定義樣式,實現超出部分省略號:
.con_txt { white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
2.el-tooltip組件本身就是懸浮提示功能,但是我們需要給超出的文本加提示,沒超出的不加提示,所以對組件進行二次封裝:
<template>
<div class="text-tooltip">
<el-tooltip class="item" effect="dark" :disabled="isShowTooltip" :content="content" placement="top">
<p class="over-flow" :class="className" @mouseover="onMouseOver(refName)">
<span :ref="refName">{{content||'-'}}</span>
</p>
</el-tooltip>
</div>
</template>
<script>
export default {
name: 'textTooltip',
props: {
// 顯示的文字內容
content: {
type: String,
default: () => {
return ''
}
},
// 外層框的樣式,在傳入的這個類名中設置文字顯示的寬度
className: {
type: String,
default: () => {
return ''
}
},
// 為頁面文字標識(如在同一頁面中調用多次組件,此參數不可重復)
refName: {
type: String,
default: () => {
return ''
}
}
},
data() {
return {
isShowTooltip: true
}
},
methods: {
onMouseOver(str) {
let parentWidth = this.$refs[str].parentNode.offsetWidth;
let contentWidth = this.$refs[str].offsetWidth;
// 判斷是否開啟tooltip功能
if (contentWidth>parentWidth) {
this.isShowTooltip = false;
} else {
this.isShowTooltip = true;
}
}
}
}
</script>
<style lang="scss" scoped>
.over-flow {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.wid190 {
width: 100%;
}
p{
margin: 0;
}
</style>
3.在需要用到組件的頁面中引入:
import tooltipOver from './components/tooltipOver'
4.使用組件:
<tooltip-over :content="tipText" class="wid190" refName="tooltipOver" ></tooltip-over>
tip:當同一頁面使用多次組件時,需要定義不同的refName屬性
