使用Vue + Tippy.js打造高定制浮層提示組件(Tooltip/Popover)
一、組件簡介與技術(shù)選型
在現(xiàn)代前端開發(fā)中,浮層提示組件(Tooltip / Popover)幾乎在每個項目中都會用到。我們希望它既能樣式靈活,又能交互豐富,還要性能優(yōu)秀、易于封裝。
本次實戰(zhàn)將基于以下技術(shù)構(gòu)建高定制提示組件:
- Vue 3:組合式 API + 插槽系統(tǒng),便于封裝彈性強的提示組件;
- Tippy.js:功能完善、主題豐富、交互穩(wěn)定的輕量彈出庫;
- Floating UI:Tippy.js 背后的定位引擎,性能優(yōu)秀,支持智能翻轉(zhuǎn)、偏移、邊界控制等特性。
二、核心功能實現(xiàn)
2.1 安裝依賴
npm install @tippyjs/vue@6 tippy.js
2.2 基礎(chǔ)封裝組件(BaseTooltip.vue)
<!-- components/BaseTooltip.vue -->
<template>
<Tippy
:content="content"
:interactive="interactive"
:placement="placement"
:theme="theme"
:trigger="trigger"
:animation="animation"
:arrow="arrow"
>
<template #default>
<slot />
</template>
<template v-if="$slots.content" #content>
<slot name="content" />
</template>
</Tippy>
</template>
<script setup>
import { Tippy } from '@tippyjs/vue';
defineProps({
content: String,
placement: { type: String, default: 'top' },
theme: { type: String, default: 'light' },
trigger: { type: String, default: 'mouseenter focus' },
animation: { type: String, default: 'shift-away' },
arrow: { type: Boolean, default: true },
interactive: { type: Boolean, default: false },
});
</script>
<style scoped>
.tippy-box[data-theme~='light'] {
background-color: white;
color: black;
border: 1px solid #ddd;
}
</style>
2.3 基本用法
<BaseTooltip content="提示信息"> <button>懸停我</button> </BaseTooltip>
2.4 插入自定義 HTML 內(nèi)容
<BaseTooltip interactive>
<template #default>
<button>點擊查看</button>
</template>
<template #content>
<div style="padding: 8px">
<h4>Popover 標題</h4>
<p>支持插槽內(nèi)容</p>
<button @click="handleClick">按鈕</button>
</div>
</template>
</BaseTooltip>
<script setup>
function handleClick() {
alert('按鈕被點擊');
}
</script>
三、功能拓展與優(yōu)化建議(附實現(xiàn))
3.1 支持延遲顯示和關(guān)閉
<BaseTooltip content="延遲提示" :delay="[500, 300]"> <span>鼠標懸停 500ms 后出現(xiàn)</span> </BaseTooltip>
3.2 支持手動控制浮層顯隱
<template>
<button ref="btnRef" @click="showTip">手動觸發(fā)</button>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import tippy from 'tippy.js';
const btnRef = ref(null);
let instance;
const showTip = () => {
if (instance) {
instance.show();
setTimeout(() => instance.hide(), 2000);
}
};
onMounted(() => {
instance = tippy(btnRef.value, {
content: '這是手動控制的提示',
trigger: 'manual'
});
});
</script>
3.3 響應(yīng)式提示內(nèi)容
<script setup>
import { ref } from 'vue';
const tipContent = ref('加載中...');
setTimeout(() => {
tipContent.value = '接口加載完成';
}, 1500);
</script>
<template>
<BaseTooltip :content="tipContent">
<span>動態(tài)內(nèi)容提示</span>
</BaseTooltip>
</template>
3.4 支持圖片和圖標
<BaseTooltip interactive>
<template #default>
<span>預(yù)覽圖片</span>
</template>
<template #content>
<img src="https://picsum.photos/120/80" alt="圖像預(yù)覽" />
</template>
</BaseTooltip>
3.5 支持關(guān)鍵詞提示推薦
<BaseTooltip interactive>
<template #default>
<span>搜索建議</span>
</template>
<template #content>
<ul style="padding: 8px;">
<li>Vue 3</li>
<li>Vite</li>
<li>Tippy.js</li>
</ul>
</template>
</BaseTooltip>
3.6 Popover 中支持交互式內(nèi)容提交
<BaseTooltip interactive trigger="click">
<template #default>
<button>點擊填寫</button>
</template>
<template #content>
<form @submit.prevent="submit">
<input v-model="msg" placeholder="輸入信息" />
<button type="submit">提交</button>
</form>
</template>
</BaseTooltip>
<script setup>
import { ref } from 'vue';
const msg = ref('');
const submit = () => {
alert('你輸入了:' + msg.value);
};
</script>
3.7 支持點擊浮層內(nèi)容進行下載(如導出圖片)
<BaseTooltip interactive trigger="click">
<template #default>
<span>點擊下載圖片</span>
</template>
<template #content>
<img
src="https://picsum.photos/100"
alt="預(yù)覽圖"
style="cursor: pointer"
@click="download"
/>
</template>
</BaseTooltip>
<script setup>
function download() {
const link = document.createElement('a');
link.;
link.download = 'preview.jpg';
link.click();
}
</script>
四、封裝為插件并全局注冊
4.1 tooltip.plugin.ts
import BaseTooltip from '@/components/BaseTooltip.vue';
export default {
install(app) {
app.component('BaseTooltip', BaseTooltip);
}
}
4.2 main.ts 中使用
import TooltipPlugin from './plugins/tooltip.plugin'; app.use(TooltipPlugin);
五、總結(jié)
本項目基于 Vue 3 和 Tippy.js 實現(xiàn)了一個高靈活性、主題豐富、插槽支持良好的浮層提示組件,適合用于:
- 基本 Tooltip 提示
- Popover 彈出內(nèi)容
- 表單浮層、關(guān)鍵詞推薦、圖像預(yù)覽等場景
可作為彈出層組件的基礎(chǔ)能力,進行下一級封裝,如 Popconfirm、Dropdown、ContextMenu、快捷操作工具條等.
到此這篇關(guān)于使用Vue + Tippy.js打造高定制浮層提示組件(Tooltip/Popover)的文章就介紹到這了,更多相關(guān)Vue Tippy.js浮層提示組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue和relation-graph庫打造高質(zhì)量的關(guān)系圖應(yīng)用程序
這篇文章主要介紹了Vue和relation-graph庫打造高質(zhì)量的關(guān)系圖應(yīng)用程序,在這篇文章中,我們深入探討了如何使用Vue和relation-graph高效打造關(guān)系圖,我們詳細介紹了數(shù)據(jù)準備、關(guān)系映射、點擊事件等關(guān)鍵步驟,需要的朋友可以參考下2023-09-09
vue項目在運行npm run build時卡住不動問題及解決方案
這篇文章主要介紹了vue項目在運行npm run build時卡住不動問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
使用Vue Router進行路由組件傳參的實現(xiàn)方式
Vue Router 為 Vue.js 應(yīng)用提供了完整的路由解決方案,其中包括了組件間的數(shù)據(jù)傳遞功能,通過路由組件傳參,我們可以輕松地在導航到新頁面時傳遞必要的數(shù)據(jù),本文將深入探討如何使用 Vue Router 進行路由組件間的傳參,并通過多個示例來展示其實現(xiàn)方式2024-09-09

