function showTitle(el, title) {
const popover = getPopover()
const popoverStyle = popover.style if (title === undefined) {
popoverStyle.display = 'none'
} else {
const elRect = el.getBoundingClientRect()
const elComputedStyle = window.getComputedStyle(el, null)
const rightOffset = parseInt(elComputedStyle.getPropertyValue('padding-right')) || 0
const topOffset = parseInt(elComputedStyle.getPropertyValue('padding-top')) || 0 popoverStyle.visibility = 'hidden'
popoverStyle.display = 'block'
popover.querySelector('.popover-content').textContent = title
popoverStyle.left = elRect.left - popover.offsetWidth / 2 + (el.offsetWidth - rightOffset) / 2 + 'px'
popoverStyle.top = elRect.top - popover.offsetHeight + topOffset + 'px'
popoverStyle.display = 'block'
popoverStyle.visibility = 'visible'
}
} function getPopover() {
let popover = document.querySelector('.title-popover') if (!popover) {
const tpl = `
<div class="popover title-popover top fade in" style="position:fixed;">
<div class="arrow"></div>
<div class="popover-content"></div>
</div>
`
const fragment = document.createRange().createContextualFragment(tpl) document.body.appendChild(fragment)
popover = document.querySelector('.title-popover')
} return popover
}
export default {
bind(el, binding, vnode) {
// 使用 const 声明一个只读的常量,其值是需要监听的事件类型列表
const events = ['mouseenter', 'mouseleave', 'click']
// 声明一个处理器,以根据不同的事件类型传不同的参数
const handler = (event) => {
if (event.type === 'mouseenter') {
// 显示一个提示框
showTitle(el, binding.value)
} else {
// 隐藏一个提示框
showTitle()
}
} // 在 el 元素上添加事件监听
events.forEach((event) => {
el.addEventListener(event, handler, false)
}) // 在 el 元素上添加一个属性,以在其他钩子进行访问
el.destroy = () => {
// 移除 el 元素上的事件监听
events.forEach((event) => {
el.removeEventListener(event, handler, false)
})
// 移除 el 元素上的 destroy
el.destroy = null
}
},
unbind(el) {
// 移除事件监听和数据绑定
el.destroy()
}
}

使用步骤:

1.建立指令;

2.在要使用的组件中引入并注册指令

 import title from '@/directives/title'
directives: {
title
}

3.页面中使用,将原来的:title=""改为v-title:

 <a v-for="item in contacts" v-title="item.title" :href="item.link" :style="contactStyle" target="_blank">
<i :class="`fa fa-${item.icon}`"></i>
</a>

指令的基础知识补充:

一个指令定义对象可以提供如下几个钩子函数(均为可选):

  • bind:只调用一次,指令第一次绑定到元素时调用,在这里可以进行一次性的初始化设置;
  • inserted:被绑定元素插入父节点时调用;
  • update:所在组件的 VNode(虚拟节点)更新时调用,但是可能发生在其子 VNode 更新之前;
  • componentUpdated:指令所在组件的 VNode 及其子 VNode 全部更新后调用;
  • unbind:只调用一次,指令与元素解绑时调用,在这里可以移除绑定的事件和其他数据;
  • 指令钩子函数会被传入以下参数:

    • el:指令所绑定的元素,可以用来操作 DOM ;
    • binding:一个对象,binding.value 表示指令的绑定值,如 v-title="'我是标题'" 中,绑定值为'我是标题'
    • vnode:Vue 编译生成的虚拟节点;
    • oldVnode:上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用。

模拟元素的title属性,自定义Vue指令的更多相关文章

  1. Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制

    一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...

  2. 手写一个关于title属性自定义提示框解决浏览器(IE)不兼容问题

    <html> <head> <meta charset="utf-8"> <title>无标题页</title> < ...

  3. 为元素添加 title 属性

    ---恢复内容开始--- 可以使用title属性(不要与title元素混淆)为网站上任何部分加上提示标签. ... <ul title="Table of Contents" ...

  4. 一个能拖动,能调整大小,能更新bind值的vue指令-vuedragx

    一. 背景说明 开发一个可自定义组件化门户配置页面,期间采用了vue框架作为前端视图引擎,作为一个刚入手vue的萌新,开发第一个功能就遇到了拦路虎.需要一个拖动并且可改变大小的容器盒子.当时查看vue ...

  5. 正确使用HTML title属性

    如果你想对使用手机,平板电脑和辅助技术的用户隐藏某些内容,而只对键盘用户显示,那么请使用title属性. 细节 HTML的title属性本身有问题.之所以有问题是因为它在一些重要的方面表现的不够好,尽 ...

  6. vue指令详解和自定义指令

    在vue中,指令以v-开头,是一种特殊的自定义行间属性,指令的职责就是其表达式的值改变时相应地将某些行为应用到DOM上 指令使用的示例 在下面的运行结果中可以看到,v-html是可以解析html标签的 ...

  7. 【CSS进阶】伪元素的妙用2 - 多列均匀布局及title属性效果

    最近无论是工作还是自我学习提升都很忙,面对长篇大论的博文总是心有余而力不足,但又不断的接触学习到零碎的但是很有意义的知识点,很想分享给大家,所以本篇可能会很短. 本篇接我另一篇讲述 CSS 伪元素的文 ...

  8. 自定义类似于Jquery UI Selectable 的Vue指令v-selectable

    话不多说,先看效果. 其实就是一个可以按住鼠标进行一个区域内条目选择的功能,相信用过Jquery UI 的都知道这是selectable的功能,然而我们如果用Vue开发的话没有类似的插件,当然你仍然可 ...

  9. Vue框架(一)——Vue导读、Vue实例(挂载点el、数据data、过滤器filters)、Vue指令(文本指令v-text、事件指令v-on、属性指令v-bind、表单指令v-model)

    Vue导读 1.Vue框架 vue是可以独立完成前后端分离式web项目的js框架 三大主流框架之一:Angular.React.Vue vue:结合其他框架优点.轻量级.中文API.数据驱动.双向绑定 ...

随机推荐

  1. 网络游戏MMORPG服务器架构

    转载于:http://justdo2008.iteye.com/blog/1936795 1.网络游戏MMORPG整体服务器框架,包括早期,中期,当前的一些主流架构 .关键词 网络协议 网络IO 消息 ...

  2. 解决引用openssl静态库libcrypto.a和libssl.a出现undefined reference to错误的问题

    最近在做使用openssl链接http和https的项目,编译时出现以下问题. /usr/local/openssl/lib/libcrypto.a(async.o): In function `as ...

  3. 数字锁相环Octave仿真

    clc; clear all; % 仿真数据长度 SimLens = 1000; % 载波信号 Fs = 2400; Ts = 1 / Fs; Fsig = 60; % 随机初相 Delta_Phas ...

  4. SQL Server 2012不支持Microsoft Visual Studio Test Controller 2010

    折腾了一个上午, 发现Test Controller怎么都连不上SQL. 能尝试的都尝试了, 觉得应该看看是不是有不支持的问题.   找到了这篇. TFS 2010 will not support ...

  5. 【java web】--css+div总结

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  6. Java网络爬虫 - 一个简单的爬虫例子

    WikiScraper.java package master.haku.scrape; import org.jsoup.Jsoup; import org.jsoup.nodes.Document ...

  7. 在div 底部显示背景图片

    下面代码实现div层背景图片在底部显示: div { background : url (/images/bg.jpg) no-repeat fixed ; background-position-y ...

  8. [ACM] ZOJ Martian Addition (20进制的两个大数相加)

    Martian Addition Time Limit: 2 Seconds      Memory Limit: 65536 KB   In the 22nd Century, scientists ...

  9. Absolute positioning

    The programmer specifies the position and the size of each widget in pixels. When you use absolute p ...

  10. qs.js库 使用方法

    1.qs.js库说明 qs是一个url参数转化(parse和stringify)的js库. https://www.npmjs.com/package/qs 2.使用(以vue文件做示例) (1)基本 ...