vue?Element?UI擴展內(nèi)容過長使用tooltip顯示
1. 介紹
Tooltip常用于展示鼠標(biāo) hover 時的提示信息。
而在實際過程中,有這么一個需求:只有文字內(nèi)容排不下,出現(xiàn)省略號,才需要顯示tooltip的提示內(nèi)容。
本文章的思路是通過一個自定義指令實現(xiàn)如下效果:姓名字段過長時才顯示tooltip
2. element-ui(vue2版本)
2.1 注冊指令
1) akTooltipAutoShow.js
說明:
注冊了一個名稱為 'ak-tooltip-auto-show' 的指令。會根據(jù)內(nèi)容進行判斷是否展示tooltip。
import Vue from 'vue'; import { on, off, getStyle } from 'element-ui/src/utils/dom'; /** * ak-tooltip-auto-show * 當(dāng)text沒有被折疊時,不顯示tooltip */ Vue.directive('ak-tooltip-auto-show', { inserted: function (el, binding, vnode) { el.addEventListener('mouseenter', function (e) { let defalutSilent = !!Vue.config.silent; Vue.config.silent = true; vnode.componentInstance.disabled = true; const range = document.createRange(); range.setStart(el, 0); range.setEnd(el, el.childNodes.length); const rangeWidth = Math.round(range.getBoundingClientRect().width); const padding = (parseInt(getStyle(el, 'paddingLeft'), 10) || 0) + (parseInt(getStyle(el, 'paddingRight'), 10) || 0); if (rangeWidth + padding > el.offsetWidth || el.scrollWidth > el.offsetWidth) { vnode.componentInstance.disabled = false; } Vue.config.silent = defalutSilent; }); } });
2.2 使用
說明:
使用<el-tooltip v-ak-tooltip-auto-show>包囊展示的內(nèi)容。
<div class="details-inner__row"> <span class="details-inner__row-name">姓名:</span> <el-tooltip placement="top" effect="dark" :content="item.name" v-ak-tooltip-auto-show> <span class="details-inner__row-value">{{item.name}}</span> </el-tooltip> </div> // css .details-inner__row-value { width: 80%; display: inline-block; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
3. element-plus(vue3版本)
3.1 注冊指令
1) akTooltipAutoShow.js
說明:
注冊了一個名稱為 'ak-tooltip-auto-show' 的指令。會根據(jù)內(nèi)容進行判斷是否展示tooltip。
注意:
因為element-plus與之前的element-ui不一樣,在element-plus(vue3版本)中是 <展示組件>包含<el-tooltip>的,所以代碼中需要在<展示組件>內(nèi)查找<el-tooltop>。
import { getStyle } from 'element-plus/lib/utils/dom/index'; /** * show-overflow-tooltip for text * 當(dāng)text沒有被折疊時,不顯示tooltip */ const akTooltipAutoShow = { created(el, binding, vnode) { let tooltipNode = vnode.children.find((childrenCmpt) => childrenCmpt.component?.type.name == 'ElTooltip'); if (tooltipNode) { let { content } = tooltipNode.props; if (content && ['添加', '編輯', '刪除','查看'].includes(content)) { el.addEventListener('click', function (e) { let defalutDisabled = tooltipNode.component.props.disabled; if (!defalutDisabled) { tooltipNode.component.props.disabled = true; setTimeout(() => { tooltipNode.component.props.disabled = defalutDisabled; }, 200); } }); } else { el.addEventListener('mouseenter', (e) => { tooltipNode.component.props.disabled = true; const range = document.createRange(); range.setStart(el, 0); range.setEnd(el, el.childNodes.length); const rangeWidth = Math.round(range.getBoundingClientRect().width); const padding = (parseInt(getStyle(el, 'paddingLeft'), 10) || 0) + (parseInt(getStyle(el, 'paddingRight'), 10) || 0); if (rangeWidth + padding > el.offsetWidth || el.scrollWidth > el.offsetWidth) { tooltipNode.component.props.disabled = false; } }); } } } }; export default akTooltipAutoShow;
2) 進行局部或全局注冊
// main.js const app = createApp(App); import akTooltipAutoShow from './akTooltipAutoShow.js'; app.directive('ak-tooltip-auto-show', akTooltipAutoShow);
3.2 使用
說明:
因element-plus(vue3版本)本身不提供 root element,所以需要把此指令放到<el-tooltip>的父級組件。
<div class="details-inner__row"> <span class="details-inner__row-name">姓名:</span> <span class="details-inner__row-value" v-ak-tooltip-auto-show> <el-tooltip placement="top-end" :content="item.name">{{item.name}}</el-tooltip> </span> </div>
css
.details-inner__row-value { width: 80%; display: inline-block; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
以上就是vue Element UI擴展內(nèi)容過長使用tooltip顯示示的詳細(xì)內(nèi)容,更多關(guān)于vue Element UI內(nèi)容過長tooltip顯示的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue 路由子組件created和mounted不起作用的解決方法
今天小編就為大家分享一篇vue 路由子組件created和mounted不起作用的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue2?Observer實例dep和閉包中dep區(qū)別詳解
這篇文章主要為大家介紹了Vue2?Observer實例dep和閉包中dep區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10