溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue中自定義指令怎么用

發布時間:2021-12-16 11:13:25 來源:億速云 閱讀:183 作者:小新 欄目:開發技術

小編給大家分享一下vue中自定義指令怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

1、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
      }
    }
  }
})

2、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>

3、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" >是的</div>

4、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中自定義指令怎么用”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女