vue中4個自定義指令講解及實例用法
四個實用的vue自定義指令
1、v-drag
需求:鼠標拖動元素
思路:
元素偏移量 = 鼠標滑動后的坐標 - 鼠標初始點擊元素時的坐標 + 初始點擊時元素距離可視區(qū)域的top、left。
將可視區(qū)域作為邊界,限制在可視區(qū)域里面拖拽?!鞠嚓P推薦:《vue.js教程》】
代碼:
Vue.directive('drag', { inserted(el) { let header = el.querySelector('.dialog_header') header.style.cssText += ';cursor:move;' header.onmousedown = function (e) { //獲取當前可視區(qū)域?qū)挕⒏? let clientWidth = document.documentElement.clientWidth let clientHeight = document.documentElement.clientHeight //獲取自身寬高 let elWidth = el.getBoundingClientRect().width let elHeight = el.getBoundingClientRect().height //獲取當前距離可視區(qū)域的top、left let elTop = el.getBoundingClientRect().top let elLeft = el.getBoundingClientRect().left //獲取點擊時候的坐標 let startX = e.pageX let startY = e.pageY document.onmousemove = function (e) { //元素偏移量 = 鼠標滑動后的坐標 - 鼠標初始點擊元素時的坐標 + 初始點擊時元素距離可視區(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
需求:后臺字段限制了長度,并且區(qū)分中英文,中文兩個字節(jié),英文一個字節(jié);所以輸入框需要限制輸入的字數(shù)并且區(qū)分字節(jié)數(shù),且需回顯已輸入的字數(shù)。
思路:
一個字節(jié)的正則/[\x00-\xff]/g
創(chuàng)建包裹字數(shù)限制的元素,并定位布局在textarea和input框上
分別計算輸入的字符一個字節(jié)的有enLen個,兩個字節(jié)的有cnLen個;用來后面字符串截斷處理
當輸入的字數(shù)超過限定的字數(shù),截斷處理;substr(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 } } //更新當前輸入框的字節(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 } } //更新當前輸入框的字節(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)建包裹字數(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 // 字數(shù)限制的盒子插入到el后是最后一個元素 el.children[el.children.length-1].innerHTML = val.length + '/' + value if(val.length>value){ let cnLen = 0 //一個字節(jié)的字數(shù) let enLen = 0 //兩個字節(jié)的字數(shù) if(val.match(/[^**]/g) && val.match(/[^**]/g).length){ enLen = val.match(/[^**]/g).length // 計算一個字節(jié)的字數(shù) //一個字節(jié)兩個字節(jié)都有的情況 if((value - val.match(/[^**]/g).length)>0){ cnLen = Math.floor((value - val.match(/[^**]/g).length)/2) }else{ cnLen = 0 } }else{ //全部兩個字節(jié)的情況 enLen = 0 cnLen = Math.floor(value/2) } if(enLen>value){ enLen = value } //超過限定字節(jié)數(shù)則截取 el.children[current].value = el.children[current].value.substr(0,enLen+cnLen) //更新當前輸入框的字節(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
需求:點擊某個元素(通常是標題、副標題之類的),動畫滾動到對應的內(nèi)容塊
思路:
定時器使用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(){ // 當前元素距離可視區(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)角色添加或刪除相應元素
代碼:
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中4個自定義指令講解及實例用法的文章就介紹到這了,更多相關vue中值得了解的4個自定義指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue3中利用Export2Excel將數(shù)據(jù)導出為excel表格
這篇文章主要給大家介紹了關于vue3中利用Export2Excel將數(shù)據(jù)導出為excel表格的相關資料,最近項目需要前端來導出Excel操作,所以給大家總結(jié)下,需要的朋友可以參考下2023-09-09基于Vue 實現(xiàn)一個中規(guī)中矩loading組件
這篇文章主要介紹了基于Vue 實現(xiàn)一個中規(guī)中矩loading組件,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04Vue3+TS+Vite+NaiveUI搭建一個項目骨架實現(xiàn)
本文主要介紹了Vue3+TS+Vite+NaiveUI搭建一個項目骨架實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06vue、uniapp實現(xiàn)組件動態(tài)切換效果
在Vue中,通過使用動態(tài)組件,我們可以實現(xiàn)組件的動態(tài)切換,從而達到頁面的動態(tài)展示效果,這篇文章主要介紹了vue、uniapp實現(xiàn)組件動態(tài)切換,需要的朋友可以參考下2023-10-10