分享四个实用的vue自定义指令
v-drag
需求:鼠标拖动元素
思路:
- 元素偏移量 = 鼠标滑动后的坐标 - 鼠标初始点击元素时的坐标 + 初始点击时元素距离可视区域的top、left
- 将可视区域作为边界,限制在可视区域里面拖拽
代码:
Vue.directive('drag', {
inserted(el) {
let header = el.querySelector('.dialog_header')
header.style.cssText += ';cursor:move;'
header.onmousedown = function (e) {
//获取当前可视区域宽、高
let clientWidth = document.documentElement.clientWidth
let clientHeight = document.documentElement.clientHeight
//获取自身宽高
let elWidth = el.getBoundingClientRect().width
let elHeight = el.getBoundingClientRect().height
//获取当前距离可视区域的top、left
let elTop = el.getBoundingClientRect().top
let elLeft = el.getBoundingClientRect().left
//获取点击时候的坐标
let startX = e.pageX
let startY = e.pageY
document.onmousemove = function (e) {
//元素偏移量 = 鼠标滑动后的坐标 - 鼠标初始点击元素时的坐标 + 初始点击时元素距离可视区域的top、left
let moveX = e.pageX - startX + elLeft
let moveY = e.pageY - startY + elTop
//将可视区域作为边界,限制在可视区域里面拖拽
if ((moveX + elWidth) > clientWidth || moveX < 0 || (moveY + elHeight) > clientHeight || moveY < 0) {
return
}
el.style.cssText += 'top:' + moveY + 'px;left:' + moveX + 'px;'
}
document.onmouseup = function () {
document.onmousemove = null
document.onmouseup = null
}
}
}
})
v-wordlimit
需求:后台字段限制了长度,并且区分中英文,中文两个字节,英文一个字节;所以输入框需要限制输入的字数并且区分字节数,且需回显已输入的字数。
思路:
- 一个字节的正则/[\x00-\xff]/g
- 创建包裹字数限制的元素,并定位布局在textarea和input框上
- 分别计算输入的字符一个字节的有enLen个,两个字节的有cnLen个;用来后面字符串截断处理
- 当输入的字数超过限定的字数,截断处理;substr(0,enLen+cnLen)
- 接口更新了输入框的值,或者初始化输入框的值,需要回显正确的字节数
代码:
Vue.directive('wordlimit',{
bind(el,binding){
console.log('bind');
let { value } = binding
Vue.nextTick(() =>{
//找到输入框是textarea框还是input框
let current = 0
let arr = Array.prototype.slice.call(el.children)
for (let i = 0; i < arr.length; i++) {
if(arr[i].tagName=='TEXTAREA' || arr[i].tagName=='INPUT'){
current = i
}
}
//更新当前输入框的字节数
el.children[el.children.length-1].innerHTML = el.children[current].value.replace(/[^\x00-\xff]/g,'**').length +'/'+value//eslint-disable-line
})
},
update(el,binding){
console.log('update');
let { value } = binding
Vue.nextTick(() =>{
//找到输入框是textarea框还是input框
let current = 0
let arr = Array.prototype.slice.call(el.children)
for (let i = 0; i < arr.length; i++) {
if(arr[i].tagName=='TEXTAREA' || arr[i].tagName=='INPUT'){
current = i
}
}
//更新当前输入框的字节数
el.children[el.children.length-1].innerHTML = el.children[current].value.replace(/[^\x00-\xff]/g,'**').length +'/'+value//eslint-disable-line
})
},
inserted(el,binding){
console.log('inserted');
let { value } = binding
//找到输入框是textarea框还是input框
let current = 0
let arr = Array.prototype.slice.call(el.children)
for (let i = 0; i < arr.length; i++) {
if(arr[i].tagName=='TEXTAREA' || arr[i].tagName=='INPUT'){
current = i
}
}
//创建包裹字数限制的元素,并定位布局在textarea和input框上
let div = document.createElement('div')
if(el.children[current].tagName=='TEXTAREA'){//是textarea,定位在右下角
div.style = 'color:#909399;position:absolute;font-size:12px;bottom:5px;right:10px;'
}else{
let styStr = ''
if(!el.classList.contains('is-disabled')){//input框不是置灰的状态则添加背景颜色
styStr = 'background:#fff;'
}
div.style = 'color:#909399;position:absolute;font-size:12px;bottom:2px;right:10px;line-height:28px;height:28px;'+styStr
}
div.innerHTML = '0/'+ value
el.appendChild(div)
el.children[current].style.paddingRight = '60px'
el.oninput = () =>{
let val = el.children[current].value
val = val.replace(/[^\x00-\xff]/g,'**') //eslint-disable-line
// 字数限制的盒子插入到el后是最后一个元素
el.children[el.children.length-1].innerHTML = val.length + '/' + value
if(val.length>value){
let cnLen = 0 //一个字节的字数
let enLen = 0 //两个字节的字数
if(val.match(/[^**]/g) && val.match(/[^**]/g).length){
enLen = val.match(/[^**]/g).length // 计算一个字节的字数
//一个字节两个字节都有的情况
if((value - val.match(/[^**]/g).length)>0){
cnLen = Math.floor((value - val.match(/[^**]/g).length)/2)
}else{
cnLen = 0
}
}else{ //全部两个字节的情况
enLen = 0
cnLen = Math.floor(value/2)
}
if(enLen>value){
enLen = value
}
//超过限定字节数则截取
el.children[current].value = el.children[current].value.substr(0,enLen+cnLen)
//更新当前输入框的字节数
el.children[el.children.length-1].innerHTML = el.children[current].value.replace(/[^\x00-\xff]/g,'**').length +'/'+value//eslint-disable-line
}
}
},
})
使用:
<el-input type="textarea" rows="3" v-wordlimit="20" v-model="value"></el-input>
v-anthor
需求:点击某个元素(通常是标题、副标题之类的),动画滚动到对应的内容块
思路:
- 定时器使用window.scrollBy
- 不考虑ie的话,可直接使用 window.scrollBy({ top: ,left:0,behavior:'smooth' })
代码:
Vue.directive('anchor',{
inserted(el,binding){
let { value } = binding
let timer = null
el.addEventListener('click',function(){
// 当前元素距离可视区域顶部的距离
let currentTop = el.getBoundingClientRect().top
animateScroll(currentTop)
},false)
function animateScroll(currentTop){
if(timer){
clearInterval(timer)
}
let c = 9
timer = setInterval(() =>{
if(c==0){
clearInterval(timer)
}
c--
window.scrollBy(0,(currentTop-value)/10)
},16.7)
}
}
})
使用:
<div class="box" v-anchor="20" style="color:red;">是的</div>
v-hasRole
需求:根据系统角色添加或删除相应元素
代码:
Vue.directive('hasRole',{
inserted(el,binding){
let { value } = binding
let roles = JSON.parse(sessionStorage.getItem('userInfo')).roleIds
if(value && value instanceof Array && value.length>0){
let hasPermission = value.includes(roles)
if(!hasPermission){
el.parentNode && el.parentNode.removeChild(el)
}
}else{
throw new Error(`请检查指令绑定的表达式,正确格式例如 v-hasRole="['admin','reviewer']"`)
}
}
})
分享四个实用的vue自定义指令的更多相关文章
- 使用Vue自定义指令实现Select组件
完成的效果图如下: 一.首先,我们简单布局一下: <template> <div class="select"> <div class="i ...
- vue 自定义指令的魅力
[第1103期]vue 自定义指令的魅力 点点 前端早读课 2017-11-08 前言 很多事情不能做过多的计划,因为计划赶不上变化.今日早读文章由富途@点点翻译分享. 正文从这开始- 在你初次接触一 ...
- vue自定义指令
Vue自定义指令: Vue.directive('myDr', function (el, binding) { el.onclick =function(){ binding.value(); } ...
- vue 自定义指令的使用案例
参考资料: 1. vue 自定义指令: 2. vue 自定义指令实现 v-loading: v-loading,是 element-ui 组件库中的一个用于数据加载过程中的过渡动画指令,项目中也很少需 ...
- vue自定义指令(Directive中的clickoutside.js)的理解
阅读目录 vue自定义指令clickoutside.js的理解 回到顶部 vue自定义指令clickoutside.js的理解 vue自定义指令请看如下博客: vue自定义指令 一般在需要 DOM 操 ...
- Vue自定义指令报错:Failed to resolve directive: xxx
Vue自定义指令报错 Failed to resolve directive: modle 这个报错有2个原因: 1.指令单词拼错 2.Vue.directive() 这个方法没有写在 new Vue ...
- vue自定义指令clickoutside使用以及扩展用法
vue自定义指令clickoutside使用以及扩展用法 产品使用vue+element作为前端框架.在功能开发过程中,难免遇到使用element的组件没办法满足特殊的业务需要,需要对其进行定制,例如 ...
- vue自定义指令clickoutside扩展--多个元素的并集作为inside
都是个人理解,如果发现错误,恳请大家批评指正,谢谢.还有我说的会比较啰嗦,因为是以自身菜鸡水平的视角来记录学习理解的过程,见谅. 1.前言 产品使用vue+element作为前端框架.在功能开发过程中 ...
- 每个人都能实现的vue自定义指令
前文 先来bb一堆废话哈哈.. 用vue做项目也有一年多了.除了用别人的插件之外.自己也没尝试去封装指令插件之类的东西来用. 刚好最近在项目中遇到一个问题.(快速点击按钮多次触发多次绑定的方法),于是 ...
- vue自定义指令,比onerror更优雅的方式实现当图片加载失败时使用默认图,提供三种方法
首先,来看下效果图(演示一下图片正常加载与加载失败时的效果) 在线体验地址:https://hxkj.vip/demo/vueImgOnerror/ 一.常规方法解决 我们都知道,img标签支持one ...
随机推荐
- Tomcat 9.0.26 高并发场景下DeadLock问题排查与修复
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/-OcCDI4L5GR8vVXSYhXJ7w作者:黄卫兵.陈锦霞 一.Tomcat容器 9.0. ...
- Apache ShardingSphere 实现分库分表及读写分离
本文为博主原创,未经允许不得转载: 项目demo 源码地址:https://gitee.com/xiangbaxiang/apache-shardingjdbc 1. 创建Maven项目,并配置 po ...
- Angular系列教程之路由守卫
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- Laravel - Could not open input file: artisan 的解决方法
cd 到 laravel的目录中执行 就可以了
- 让vs支持wsl调试
WSL安装 wsl --install -d Ubuntu 等一会提示输入用户名,不用管它,直接关闭,下次打开wsl,会以无密码的root用户打开 wsl卸载 wsl --unregister Ubu ...
- 微信小程序——如何获取url中的数据
如何获取url中的数据 这里举个例子! 当我们需要编辑一个可以点击的图片超链接 这时候结构是这个样子的 首先最外层有一个视图层view 在view中添加超链接标签<navigator> & ...
- 【面试题精讲】说一说springboot加载配置文件优先级
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 文章更新计划 系列文章地址 Spring Boot 加载配置文 ...
- [转帖]tiup cluster reload
https://docs.pingcap.com/zh/tidb/stable/tiup-component-cluster-reload 4 Contributors 在修改集群配置之后,需要通过 ...
- [转帖]学习如何编写 Shell 脚本(基础篇)
https://juejin.cn/post/6930013333454061575 前言 如果仅仅会 Linux 一些命令,其实已经可以让你在平时的工作中游刃有余了.但如果你还会编写 Shell 脚 ...
- [转帖]AMD Zen CPU 架构以及不同CPU性能大PK
https://plantegg.github.io/2021/08/13/AMD_Zen_CPU%E6%9E%B6%E6%9E%84/ 前言 本文先介绍AMD Zen 架构,结合前一篇文章<C ...