vue自定義指令添加跟隨鼠標光標提示框v-tooltip方式
自定義指令添加跟隨鼠標光標提示框v-tooltip
在vue中添加自定義指令,能夠識別dom,通過鼠標hover事件移入當前區(qū)域后,顯示浮層
1、directives自定義提示指令
? directives: {
? ? // 自定義提示指令
? ? tooltip: {
? ? ? componentUpdated: function(el, binding) {
? ? ? ? // 鼠標移入時,將浮沉元素插入到body中
? ? ? ? el.onmouseenter = function(e) {
? ? ? ? ? // 創(chuàng)建浮層元素并設(shè)置樣式
? ? ? ? ? const vcTooltipDom = document.createElement('div');
? ? ? ? ? vcTooltipDom.style.cssText = `
? ? ? ? ? overflow: auto;
? ? ? ? ? position:absolute;
? ? ? ? ? background: #fff;;
? ? ? ? ? color:#666;
? ? ? ? ? box-shadow: rgba(168, 168, 168, 0.295) 1px 2px 10px;
? ? ? ? ? border-radius:5px;
? ? ? ? ? padding:10px;
? ? ? ? ? display:inline-block;
? ? ? ? ? font-size:14px;
? ? ? ? ? z-index:2
? ? ? ? `;
? ? ? ? ? // 設(shè)置id方便尋找
? ? ? ? ? vcTooltipDom.setAttribute('id', 'vc-tooltip');
? ? ? ? ? // 將浮層插入到body中
? ? ? ? ? document.body.appendChild(vcTooltipDom);
? ? ? ? ? // 浮層中的文字 通過屬性值傳遞動態(tài)的顯示文案
? ? ? ? ? document.getElementById('vc-tooltip').innerHTML = el.getAttribute('tips');
? ? ? ? };
? ? ? ? // 鼠標移動時,動態(tài)修改浮沉的位置屬性
? ? ? ? el.onmousemove = function(e) {
? ? ? ? ? const vcTooltipDom = document.getElementById('vc-tooltip');
? ? ? ? ? vcTooltipDom.style.top = e.clientY + 15 + 'px';
? ? ? ? ? vcTooltipDom.style.left = e.clientX + 15 + 'px';
? ? ? ? };
? ? ? ? // 鼠標移出時將浮層元素銷毀
? ? ? ? el.onmouseleave = function() {
? ? ? ? ? // 找到浮層元素并移出
? ? ? ? ? const vcTooltipDom = document.getElementById('vc-tooltip');
? ? ? ? ? vcTooltipDom && document.body.removeChild(vcTooltipDom);
? ? ? ? };
? ? ? }
? ? }
? }通過監(jiān)聽鼠標移入移出的mouse方法,設(shè)置浮層樣式與出現(xiàn)時機
2、div顯示dom標簽v-tooltip
<div id="bar-echart" tips="共有6個任務(wù)執(zhí)行成功" v-tooltip/>
此時運行后,出現(xiàn)浮層
vue自定義指令實現(xiàn)tooltip功能
1、需求
元素展示提示框跟隨鼠標移動
2、思路
vue的自定義指令
將顯示內(nèi)容放到容器中,通過值傳遞,監(jiān)聽鼠標移入事件,鼠標移入后將容器append到body
- 監(jiān)聽鼠標移動事件,根據(jù)鼠標的e.clientY,e.clientX修改容器位置
- 監(jiān)聽鼠標移出事件,銷毀容器
3、代碼
// 自定義提示指令
directives: {
? ? tooltip(el, binding){
? ? ? ? // 鼠標移入時,將浮沉元素插入到body中
? ? ? ? el.onmouseenter = function (e) {
? ? ? ? ? // 創(chuàng)建浮層元素并設(shè)置樣式
? ? ? ? ? const vcTooltipDom = document.createElement("div");
? ? ? ? ? vcTooltipDom.style.cssText = `
?? ??? ? ? ? ? ? ?position:absolute;
?? ??? ? ? ? ? ? ?background: #fff;;
?? ??? ? ? ? ? ? ?color:#fff;
?? ??? ? ? ? ? ? ?font-size:14px;
?? ??? ? ? ? ? ? ?z-index:1000
?? ??? ??? ?;
? ? ? ? ? // 設(shè)置id方便尋找
? ? ? ? ? vcTooltipDom.setAttribute("id", "vc-tooltip");
? ? ? ? ? // 將浮層插入到body中
? ? ? ? ? document.body.appendChild(vcTooltipDom);
? ? ? ? ? // 浮層中的文字 通過屬性值傳遞動態(tài)的顯示文案
? ? ? ? ? document.getElementById("vc-tooltip").innerHTML =
? ? ? ? ? ? el.getAttribute("tips");
? ? ? ? };
? ? ? ? // 鼠標移動時,動態(tài)修改浮沉的位置屬性
? ? ? ? el.onmousemove = function (e) {
? ? ? ? ? const vcTooltipDom = document.getElementById("vc-tooltip");
? ? ? ? ? vcTooltipDom.style.top = e.clientY + 15 + "px";
? ? ? ? ? vcTooltipDom.style.left = e.clientX + 15 + "px";
? ? ? ? };
? ? ? ? // 鼠標移出時將浮層元素銷毀
? ? ? ? el.onmouseleave = function () {
? ? ? ? ? // 找到浮層元素并移出
? ? ? ? ? const vcTooltipDom = document.getElementById("vc-tooltip");
? ? ? ? ? vcTooltipDom && document.body.removeChild(vcTooltipDom);
? ? ? ? };
? ? },
? },4、在元素上使用
<div v-tooltip :tip='youtxt'></div>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用threeJs導(dǎo)入obj模型并實現(xiàn)添加標注
這篇文章主要介紹了vue使用threeJs導(dǎo)入obj模型并實現(xiàn)添加標注方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Vue.js中this如何取到data和method里的屬性詳解
methods屬性是一個對象,通常我們會在這個對象中定義很多的方法,下面這篇文章主要給大家介紹了關(guān)于Vue.js中this如何取到data和method里的屬性,需要的朋友可以參考下2022-12-12
vue中的vue-router?query方式和params方式詳解
這篇文章主要介紹了vue中的vue-router?query方式和params方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
淺談Vue3 Composition API如何替換Vue Mixins
這篇文章主要介紹了淺談Vue3 Composition API如何替換Vue Mixins,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

