vue中4個(gè)自定義指令講解及實(shí)例用法
四個(gè)實(shí)用的vue自定義指令
1、v-drag
需求:鼠標(biāo)拖動(dòng)元素
思路:
元素偏移量 = 鼠標(biāo)滑動(dòng)后的坐標(biāo) - 鼠標(biāo)初始點(diǎn)擊元素時(shí)的坐標(biāo) + 初始點(diǎn)擊時(shí)元素距離可視區(qū)域的top、left。
將可視區(qū)域作為邊界,限制在可視區(qū)域里面拖拽?!鞠嚓P(guān)推薦:《vue.js教程》】
代碼:
Vue.directive('drag', {
inserted(el) {
let header = el.querySelector('.dialog_header')
header.style.cssText += ';cursor:move;'
header.onmousedown = function (e) {
//獲取當(dāng)前可視區(qū)域?qū)?、?
let clientWidth = document.documentElement.clientWidth
let clientHeight = document.documentElement.clientHeight
//獲取自身寬高
let elWidth = el.getBoundingClientRect().width
let elHeight = el.getBoundingClientRect().height
//獲取當(dāng)前距離可視區(qū)域的top、left
let elTop = el.getBoundingClientRect().top
let elLeft = el.getBoundingClientRect().left
//獲取點(diǎn)擊時(shí)候的坐標(biāo)
let startX = e.pageX
let startY = e.pageY
document.onmousemove = function (e) {
//元素偏移量 = 鼠標(biāo)滑動(dòng)后的坐標(biāo) - 鼠標(biāo)初始點(diǎn)擊元素時(shí)的坐標(biāo) + 初始點(diǎn)擊時(shí)元素距離可視區(qū)域的top、left
let moveX = e.pageX - startX + elLeft
let moveY = e.pageY - startY + elTop
//將可視區(qū)域作為邊界,限制在可視區(qū)域里面拖拽
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
}
}
}
})
2、v-wordlimit
需求:后臺(tái)字段限制了長(zhǎng)度,并且區(qū)分中英文,中文兩個(gè)字節(jié),英文一個(gè)字節(jié);所以輸入框需要限制輸入的字?jǐn)?shù)并且區(qū)分字節(jié)數(shù),且需回顯已輸入的字?jǐn)?shù)。
思路:
一個(gè)字節(jié)的正則/[\x00-\xff]/g
創(chuàng)建包裹字?jǐn)?shù)限制的元素,并定位布局在textarea和input框上
分別計(jì)算輸入的字符一個(gè)字節(jié)的有enLen個(gè),兩個(gè)字節(jié)的有cnLen個(gè);用來(lái)后面字符串截?cái)嗵幚?br />
當(dāng)輸入的字?jǐn)?shù)超過(guò)限定的字?jǐn)?shù),截?cái)嗵幚恚籹ubstr(0,enLen+cnLen)
接口更新了輸入框的值,或者初始化輸入框的值,需要回顯正確的字節(jié)數(shù)
代碼:
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
}
}
//更新當(dāng)前輸入框的字節(jié)數(shù)
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
}
}
//更新當(dāng)前輸入框的字節(jié)數(shù)
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
}
}
//創(chuàng)建包裹字?jǐn)?shù)限制的元素,并定位布局在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框不是置灰的狀態(tài)則添加背景顏色
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
// 字?jǐn)?shù)限制的盒子插入到el后是最后一個(gè)元素
el.children[el.children.length-1].innerHTML = val.length + '/' + value
if(val.length>value){
let cnLen = 0 //一個(gè)字節(jié)的字?jǐn)?shù)
let enLen = 0 //兩個(gè)字節(jié)的字?jǐn)?shù)
if(val.match(/[^**]/g) && val.match(/[^**]/g).length){
enLen = val.match(/[^**]/g).length // 計(jì)算一個(gè)字節(jié)的字?jǐn)?shù)
//一個(gè)字節(jié)兩個(gè)字節(jié)都有的情況
if((value - val.match(/[^**]/g).length)>0){
cnLen = Math.floor((value - val.match(/[^**]/g).length)/2)
}else{
cnLen = 0
}
}else{ //全部?jī)蓚€(gè)字節(jié)的情況
enLen = 0
cnLen = Math.floor(value/2)
}
if(enLen>value){
enLen = value
}
//超過(guò)限定字節(jié)數(shù)則截取
el.children[current].value = el.children[current].value.substr(0,enLen+cnLen)
//更新當(dāng)前輸入框的字節(jié)數(shù)
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>
3、v-anthor
需求:點(diǎn)擊某個(gè)元素(通常是標(biāo)題、副標(biāo)題之類的),動(dòng)畫滾動(dòng)到對(duì)應(yīng)的內(nèi)容塊
思路:
定時(shí)器使用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(){
// 當(dāng)前元素距離可視區(qū)域頂部的距離
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>
4、v-hasRole
需求:根據(jù)系統(tǒng)角色添加或刪除相應(yīng)元素
代碼:
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(`請(qǐng)檢查指令綁定的表達(dá)式,正確格式例如 v-hasRole="['admin','reviewer']"`)
}
}
})
到此這篇關(guān)于vue中4個(gè)自定義指令講解及實(shí)例用法的文章就介紹到這了,更多相關(guān)vue中值得了解的4個(gè)自定義指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue在自定義組件上使用v-model和.sync的方法實(shí)例
自定義組件的v-model和.sync修飾符其實(shí)本質(zhì)上都是vue的語(yǔ)法糖,用于實(shí)現(xiàn)父子組件的"數(shù)據(jù)"雙向綁定,下面這篇文章主要給大家介紹了關(guān)于vue在自定義組件上使用v-model和.sync的相關(guān)資料,需要的朋友可以參考下2022-07-07
在Vue項(xiàng)目中取消ESLint代碼檢測(cè)的步驟講解
今天小編就為大家分享一篇關(guān)于在Vue項(xiàng)目中取消ESLint代碼檢測(cè)的步驟講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
vue頁(yè)面更新patch的實(shí)現(xiàn)示例
這篇文章主要介紹了vue頁(yè)面更新patch的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue中Echarts使用動(dòng)態(tài)數(shù)據(jù)的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了vue中Echarts使用動(dòng)態(tài)數(shù)據(jù)的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
用Vue實(shí)現(xiàn)頁(yè)面訪問(wèn)攔截的方法詳解
大家在做前端項(xiàng)目的時(shí)候,大部分頁(yè)面, 游客都可以直接訪問(wèn) , 如遇到 需要登錄才能進(jìn)行的操作,頁(yè)面將提示并跳轉(zhuǎn)到登錄界面,那么如何才能實(shí)現(xiàn)頁(yè)面攔截并跳轉(zhuǎn)到對(duì)應(yīng)的登錄界面呢,本文小編就來(lái)給大家介紹一下Vue實(shí)現(xiàn)頁(yè)面訪問(wèn)攔截的方法,需要的朋友可以參考下2023-08-08
基于vue+face-api.js實(shí)現(xiàn)前端人臉識(shí)別功能
基于face-api.js要實(shí)現(xiàn)人臉識(shí)別功能,首先要將自己需要的模型文件下載保存在靜態(tài)目錄下,可以通過(guò)cdn的方式在index.html中引入face-api.js,本文給大家介紹vue+face-api.js實(shí)現(xiàn)前端人臉識(shí)別功能,感興趣的朋友一起看看吧2023-12-12
vue3使用wangeditor封裝和自定義上傳文件官方教程
這篇文章主要為大家介紹了vue3使用wangeditor封裝和自定義上傳文件的官方教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-06-06

