vue3中實現(xiàn)文本顯示省略號和tooltips提示框的方式詳解
前言
在 B 端業(yè)務(wù)中,我們經(jīng)常會遇到文本內(nèi)容超出容器區(qū)域需顯示省略號的需求。當(dāng)鼠標(biāo)移入文本時,會出現(xiàn) Tooltip 顯示完整內(nèi)容。最近,我也遇到了這樣的場景。為了提高業(yè)務(wù)通用性,我已將其封裝為組件、Hook 和指令等形式供使用。
使用方式
npm install vue-ellipsis-tooltip --save
技術(shù)細(xì)節(jié)
如何判斷文本超出了父容器
- 首先創(chuàng)建一個空白的
div,將當(dāng)前父元素的樣式全部復(fù)制到這個 div 上。將全部文字填充到空白 div 中,獲取當(dāng)前全部文案的高度。然后使用定位和層級使其在當(dāng)前屏幕上不可見。 - 通過
二分法,遞歸地找出期望目標(biāo)元素的行數(shù)的高度(lineHeight * rows)所能容納的最大字符串?dāng)?shù)量。

基礎(chǔ)的偽代碼如下:
export const getEllipsisText = (parentNode: HTMLElement, fullText: string, maxHeight: number) => {
const ellipsisContainer = createMeasureContainer(parentNode)
ellipsisContainer.innerHTML = ""
const textNode = document.createTextNode(fullText)
ellipsisContainer.appendChild(textNode)
// 檢查當(dāng)前文本是否滿足高度條件
function inRange() {
return ellipsisContainer.offsetHeight <= maxHeight
}
if (inRange()) {
return {
ellipsis: false,
text: fullText
}
}
// 尋找最多的文字
function measureText(startLoc = 0, endLoc = fullText.length, lastSuccessLoc = 0) {
if (startLoc > endLoc) {
// 找到滿足條件的最長文本,清理并返回結(jié)果
const finalText = fullText.slice(0, lastSuccessLoc) + "..."
return {
ellipsis: true,
text: finalText
}
// return finalText
}
const midLoc = Math.floor((startLoc + endLoc) / 2)
textNode.textContent = fullText.slice(0, midLoc) + "..."
if (inRange()) {
return measureText(midLoc + 1, endLoc, midLoc)
} else {
return measureText(startLoc, midLoc - 1, lastSuccessLoc)
}
}
return measureText()
}
- 時間復(fù)雜度主要取決于
measureText函數(shù),它使用二分查找法來確定滿足條件的最長文本。二分查找的時間復(fù)雜度是 O(log n),其中 n 是字符串fullText的長度。在這個過程中,每一步都會將搜索范圍減半,直到找到最合適的文本長度。 - 空間復(fù)雜度由于
measureText使用了遞歸調(diào)用,空間復(fù)雜度受到遞歸深度的影響。在最壞的情況下,遞歸深度與二分查找的步驟數(shù)相同,即 O(log n)
創(chuàng)建ToolTip彈窗
ToolTip主要是通過@popperjs/core進行實現(xiàn)
怎么使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./style.css" rel="external nofollow" >
<style>
.wrapper{
margin: 0 auto;
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/3.3.4/vue.cjs.prod.min.js"></script>
<script src="./vEllipsis.js"></script>
</head>
<body>
<div id="app">
<div class="wrapper" :style="{ width: '200px' }">
<span v-ellipsis="{ rows: 1, text: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazzzzzzzzzz' }" />
</div>
<div class="wrapper" :style="{ width: `200px` }">
<vue-ellipsis-tooltip :rows="3" text=" A design is a plan or specification for the" />
</div>
</div>
<script>
const App = {
data() {
return {
message: "Hello Element Plus",
};
},
};
const app = Vue.createApp(App);
app.use(vEllipsis);
app.mount("#app");
</script>
</body>
</html>
組件形式
<template>
<div class="wrapper" :style="{ width: `${width}px` }">
<vue-ellipsis-tooltip
:rows="options.rows"
:text="options.text"
:poperOptions="{
effect: 'light'
}"/>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue"
import {vue-ellipsis-tooltip} from "vue-ellipsis-tooltip"
const options = ref({
rows: 1,
text: "你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好"
})
const width = ref(200)
</script>
指令形式
<template>
<div class="wrapper" :style="{ width: `${width}px` }">
<span ref="targetRef" v-ellipsis="{ rows: options.rows, text: options.text }" />
</div>
</template>
<script setup lang="ts">
import { ref } from "vue"
const options = ref({
rows: 1,
text: "你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好"
})
const width = ref(200)
</script>
hook形式
<template>
<div class="wrapper" :style="{ width: `${width}px` }" ref="wrapperRef">
<span ref="targetRef" />
</div>
</template>
<script setup lang="ts">
import { useEllipsis } from "vue-ellipsis-tooltip"
import { ref } from "vue"
const targetRef = ref()
const width = ref(200)
const options = ref({
rows: 1,
text: "你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好"
})
useEllipsis(targetRef, options.value, {
effect: "dark"
})
</script>
在vite中使用
main.ts
import { createApp } from 'vue'
import "vue-ellipsis-tooltip/dist/style.css"
import vEllipsis from "vue-ellipsis-tooltip"
import App from './App.vue'
const app = createApp(App)
app.use(vEllipsis)
app.mount('#app')
Attributes
| 名稱 | 說明 | 類型 | 默認(rèn)值 |
|---|---|---|---|
| rows | 顯示省略的行數(shù) | number | 1 |
| showTooltip | 配置省略時的彈出框 | boolean | false |
| text | 顯示的內(nèi)容 | string | '' |
| disabled | 是否禁用 | boolean | false |
poperOptions(彈出窗屬性)
| 名稱 | 說明 | 類型 | 默認(rèn)值 |
|---|---|---|---|
| effect | 主題 | dark / light | dark |
| teleported | 是否插入body | boolean | false |
| showArrow | 是否顯示箭頭 | boolean | true |
| popperClass | 彈出窗類名 | string | '' |
| offset | 出現(xiàn)位置的偏移量 | number | 12 |
| showAfter | 在觸發(fā)后多久顯示內(nèi)容,單位毫秒 | number | 0 |
| hideAfter | 延遲關(guān)閉,單位毫秒 | number | 200 |
| placement | Tooltip 組件出現(xiàn)的位置 | enum | 'bottom' |
| zIndex | 彈窗層級 | number | 1024 |
| popperOptions | popper.js 參數(shù) | object | {} |
總結(jié)
該組件提供了一個靈活且易于集成的方式,以滿足B端業(yè)務(wù)
以上就是vue3中實現(xiàn)文本顯示省略號和tooltips提示框的方式詳解的詳細(xì)內(nèi)容,更多關(guān)于vue3文本顯示省略號和提示框的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
element-ui中如何給el-table的某一行或某一列加樣式
本文主要介紹了element-ui中怎么給el-table的某一行或某一列加樣式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
基于vue開發(fā)微信小程序mpvue-docs跳轉(zhuǎn)頁面功能
這篇文章主要介紹了基于vue寫微信小程序mpvue-docs跳轉(zhuǎn)頁面,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
Vue+jsPlumb實現(xiàn)連線效果(支持滑動連線和點擊連線)
jsPlumb 是一個比較強大的繪圖組件,它提供了一種方法,主要用于連接網(wǎng)頁上的元素。本文將利用jsPlumb實現(xiàn)連線效果,同時支持滑動連線和點擊連線,感興趣的可以了解一下2023-01-01
關(guān)于vue3+echart5?遇到的坑?Cannot?read?properties?of?undefine
這篇文章主要介紹了vue3+echart5?遇到的坑?Cannot?read?properties?of?undefined?(reading?'type'),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04

