参考资料:

1. vue 自定义指令

2. vue 自定义指令实现 v-loading

  v-loading,是 element-ui 组件库中的一个用于数据加载过程中的过渡动画指令,项目中也很少需要自己去写自定以指令。碰巧这段时间自己练习了编写组件,完成看看能不能实现一个自定义的这样一个指令。话不多说,上代码:

<div class="table" v-loadAnimation="loading">
...
</div> <script>
export default {
data() {
return {
...
}
},
directives: {
loadAnimation: {
bind: (el, binding) => {
// console.log('bind', el, binding)
// 遮罩层
const modal = document.createElement('div');
modal.className = 'modal-loading';
// 遮罩层动画
const animation = document.createElement('div');
animation.className = 'modal-loading-animation';
modal.appendChild(animation);
// 自定义的 loadingElement 属性/其他, 下面钩子函数可以使用;
el.loadingElement = modal; const curStyle = window.getComputedStyle(el);
const position = curStyle.position; if (position === 'absolute' || position === 'relative') {
el.style.position = position;
} else {
el.style.position = 'relative';
} if (binding.value) {
el.appendChild(modal)
}
},
update: (el, binding) => {
// console.log('update', el, binding)
if (binding.value) {
if (el.loadingElement.parentNode === null) {
el.appendChild(el.loadingElement);
}
} else {
if (el === el.loadingElement.parentNode) {
el.removeChild(el.loadingElement);
}
}
},
unbind: (el) => {
if (el.loadingElement.parentNode === el) {
el.removeChild(el.loadingElement);
}
el.loadingElement = null;
}
}
},
methods: {
...
}
}
</script> <style>
/* for loading animation */
.modal-loading {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: 100;
background-color: rgba(255, 255, 255, 0.95);
overflow: hidden;
}
.modal-loading .modal-loading-animation {
width: 30px;
height: 30px;
border-radius: 50%;
border: 4px solid #1a7dff;
border-left-color: transparent;
animation: modal-loading-rotate 1s linear infinite;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
@keyframes modal-loading-rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>

  注意:

    1.这个示例中,v-loadAnimation 是以局部指令的方式注册的,在写相关 css 的时候,style 标签中不能加入 scoped 字样,否则不会触发效果;

    2.关于指令的钩子函数以及相关参数,具体移步 官方文档

vue 自定义指令的使用案例的更多相关文章

  1. vue自定义指令

    Vue自定义指令: Vue.directive('myDr', function (el, binding) { el.onclick =function(){ binding.value(); } ...

  2. vue自定义指令(Directive中的clickoutside.js)的理解

    阅读目录 vue自定义指令clickoutside.js的理解 回到顶部 vue自定义指令clickoutside.js的理解 vue自定义指令请看如下博客: vue自定义指令 一般在需要 DOM 操 ...

  3. Vue自定义指令报错:Failed to resolve directive: xxx

    Vue自定义指令报错 Failed to resolve directive: modle 这个报错有2个原因: 1.指令单词拼错 2.Vue.directive() 这个方法没有写在 new Vue ...

  4. vue自定义指令clickoutside使用以及扩展用法

    vue自定义指令clickoutside使用以及扩展用法 产品使用vue+element作为前端框架.在功能开发过程中,难免遇到使用element的组件没办法满足特殊的业务需要,需要对其进行定制,例如 ...

  5. vue自定义指令clickoutside扩展--多个元素的并集作为inside

    都是个人理解,如果发现错误,恳请大家批评指正,谢谢.还有我说的会比较啰嗦,因为是以自身菜鸡水平的视角来记录学习理解的过程,见谅. 1.前言 产品使用vue+element作为前端框架.在功能开发过程中 ...

  6. 每个人都能实现的vue自定义指令

    前文 先来bb一堆废话哈哈.. 用vue做项目也有一年多了.除了用别人的插件之外.自己也没尝试去封装指令插件之类的东西来用. 刚好最近在项目中遇到一个问题.(快速点击按钮多次触发多次绑定的方法),于是 ...

  7. vue自定义指令,比onerror更优雅的方式实现当图片加载失败时使用默认图,提供三种方法

    首先,来看下效果图(演示一下图片正常加载与加载失败时的效果) 在线体验地址:https://hxkj.vip/demo/vueImgOnerror/ 一.常规方法解决 我们都知道,img标签支持one ...

  8. Vue自定义指令使用场景

    当你第一次接触vue的时候,一定会使用到其中的几个指令,比如:v-if.v-for.v-bind...这些都是vue为我们写好的,用起来相当的爽.如果有些场景不满足,需要我们自己去自定义,那要怎么办呢 ...

  9. Vue自定义指令使用方法详解 和 使用场景

    Vue自定义指令的使用,具体内容如下 1.自定义指令的语法 Vue自定义指令语法如下: Vue.directive(id, definition) 传入的两个参数,id是指指令ID,definitio ...

随机推荐

  1. 代码编辑器横评:为什么 VS Code 能拔得头筹

    摘要: 为什么 VS Code 这么火... 公众号:玩转 VS Code 原文:代码编辑器横评:为什么 VS Code 能拔得头筹 Fundebug经授权转载,版权归原作者所有. 2015 年 4 ...

  2. vuex最详细完整的使用用法

    来自:https://blog.csdn.net/qq_35430000/article/details/79412664#commentBox  github仓库地址:https://github. ...

  3. SCSS 在项目中的运用

    最后一段时间一直在做一些网站或是CMS的项目,想用bootstrap,但是,设计那哥们说了,用什么都行,就不能用bootstrap,我去了个..... 无语中,逼着自己写.说实话,就是用bootstr ...

  4. mean项目的分模块开发

    全文字版: 新建maven工程在,作为父工程用于最后集合使用,该工程不需要src,只需要一个pom.xml文件,规定一下依赖版本之类的,再建一个工具类的工程,不需要放配置文件,和工程中方法接口有关的不 ...

  5. 关于Python, ftplib模块中的cwd()进入含中文目录失败的问题

    使用Python的ftplib模块连接ftp服务器时, 使用cwd()连接含中文的目录, 报错 : UnicodeEncodeError: 'latin-1' codec can't encode c ...

  6. 生成Csv格式的字符串

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  7. 注册表修改computer name

    修改windows server的机器名的时候,发现change按钮是disable的. 手动修改不了,用注册表regedit来修改. HKEY_LOCAL_MACHINE\SYSTEM\Curren ...

  8. 使用mybatis操作AS400数据库

    先简单说一下怎么使用[jt400.jar]连接AS400上的DB2数据库. ※ jt400.jar资源,如果有安装AS400客户端的话,参考IBM官网 ※ http://www-01.ibm.com/ ...

  9. Tmux 入门

    什么是 Tmux Tmux 官方 Wiki 简单来说,Tmux 是一个能够让你一个窗口当多个窗口使用的终端模拟器.并且你还可以将它放到后台,等到想使用的时候再使用. 为什么要用 Tmux 在服务器上调 ...

  10. MySQL 数据表创建及管理

    use stuinfo; -- 指定当前数据库 CREATE table if not exists student1( -- 创建数据表student1 sNo ) not NULL, sName ...