溫馨提示×

溫馨提示×

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

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

vue-auto-focus: 控制自動聚焦行為的 vue 指令方法

發(fā)布時間:2020-09-18 07:59:37 來源:腳本之家 閱讀:250 作者:大灰狼的小綿羊哥哥 欄目:web開發(fā)

在網(wǎng)頁的表單中,經(jīng)常需要用程序來控制input和textarea的自動聚焦行為。例如我最近做的一個項目,有個裝箱出庫的流程,input框自動聚焦的流程如下:頁面進入時自動聚焦到訂單號輸入框->訂單號掃描完畢聚焦到商品條碼輸入框->掃描完一個商品條碼后依然停留在條碼輸入框->所有條碼掃描完畢聚焦到訂單號輸入框。

為了應(yīng)付這種需求,就做了這個指令,github地址: vue-auto-focus ,歡迎star。

example

<template>
 <form v-auto-focus="focusCtrl" :data-current="currentIndex" :data-action="actionType">
  <input @focus="setFocusIndex(0)" type="text" data-index="0">
  <input @focus="setFocusIndex(1)" type="text" data-index="1">
  <textarea @focus="setFocusIndex(2)" name="" id="" cols="30" rows="10" data-index="2"></textarea>
  <input @focus="setFocusIndex(3)" type="text" data-index="3">
 </form>
</template>
<style scoped>
</style>
<script type="text/babel">
 export default {
  data() {
   return {
    focusCtrl: 0, // 自動聚焦控制,變動時,執(zhí)行自動聚焦指令
    currentIndex: 0, // 當(dāng)前聚焦元素的索引
    actionType: 'next', // 自動聚焦的行為類型
   }
  },
  methods: {
   /**
    * 控制自動聚焦指令執(zhí)行
    * @param actionType {string} 自動聚焦類型 it can be 'next'/'prev'/'first'/'last'/'jump'
    * @param index {string} 當(dāng)actionType為'jump'時,需要傳入將要聚焦元素的索引
    **/
   setFocus(actionType,index) {
    if (actionType === 'jump') {
     this.currentIndex = index
    }
    this.focusCtrl++
    this.actionType = actionType
   },
   /**
    * 元素聚焦時,獲取當(dāng)前聚焦元素的索引
    * @param index {number} 當(dāng)前聚焦的索引
    **/
   setFocusIndex(index) {
    this.currentIndex = index
   },
  }
 }
</script>

行為控制

next 聚焦到下一個元素

prev 聚焦到上一個元素

first 聚焦到第一個元素

last 聚焦到最后一個元素

jump 聚焦到指定的元素

聚焦行為控制邏輯

/**
 * 聚焦行為控制
 * next 聚焦到下一個元素
 * prev 聚焦到上一個元素
 * first 聚焦到第一個元素
 * last 聚焦到最后一個元素
 * jump 跳轉(zhuǎn)到指定的元素
 * @param el
 */
const focusCtrl = function (el) {
 const action = el.dataset.action
 const allFocusEls = getAllFocusEls(el)
 const focusLen = allFocusEls.length
 let current = getTargetIndex(el,allFocusEls)
 switch (action) {
  case 'next': // 如果action為next,則聚焦到下一個輸入框
   if (current >= focusLen - 1) {
    current = focusLen - 1
   } else {
    current++
   }
   autoFocus(allFocusEls[current])
   break
  case 'prev': // 如果action為prev,則聚焦到上一個輸入框
   if (current <= 0) {
    current = 0
   } else {
    current--
   }
   autoFocus(allFocusEls[current])
   break
  case 'first': // 如果為first,則聚焦到第一個輸入框
   current = 0
   autoFocus(allFocusEls[current])
   break;
  case 'last': // 如果為last,則聚焦到最后一個輸入框
   current = focusLen - 1
   autoFocus(allFocusEls[current])
   break
  case 'jump': // 如果為jump,則獲取focusIndex,跳轉(zhuǎn)到對應(yīng)的輸入框
   if (current >= 0 && current < focusLen) {
    autoFocus(allFocusEls[current])
   }
   break
 }
}

必須在需要控制的元素上添加data-index屬性,需要在父元素上添加data-action屬性和data-current屬性,data-action為指令行為的類型(值為next,prev等),data-current為當(dāng)前聚焦元素的data-index值, getAllFocusEls 方法其實就是獲取所有屬性為data-index的元素,代碼如下:

/**
 * 獲取需要聚焦的所有元素
 * @param el {Node} 指令掛載的元素
 * @returns {NodeList} 需要聚焦的元素列表
 */
const getAllFocusEls = function (el) {
 return el.querySelectorAll('[data-index]')
}

getTargetIndex 方法用來獲取當(dāng)前聚焦元素的在集合中的索引值,代碼如下:

/**
 * 獲取當(dāng)前聚焦元素在集合中的位置
 * @param el
 * @param collection
 * @returns {number}
 */
const getTargetIndex = function(el,collection) {
 const target = document.querySelector(`[data-index="${el.dataset.current}"]`)
 return Array.from(collection).indexOf(target)
}

inserted

指令掛載時,自動聚焦到指定的元素

/**
 * 進入頁面時,根據(jù)設(shè)置的data-index索引值,聚焦到對應(yīng)的輸入框
 * @param el
 */
inserted: function (el) {
 const allFocusEls = getAllFocusEls(el) // 獲取需要聚焦的input元素組
 let current = getTargetIndex(el,allFocusEls)
 if (!current || current < 0 || current >= allFocusEls.length) { // 如果沒有設(shè)置data-current,或者current的數(shù)值范圍不符合要求,則默認(rèn)聚焦到第一個輸入框
  current = 0
 }
 const currentEl = allFocusEls[current]
 autoFocus(currentEl)
},

update

通過指令的value值控制指令的執(zhí)行,如果值有變動,則執(zhí)行指定的操作,聚焦到指定的元素

/**
 * 更新時,如果focusCtrl有變動,則根據(jù)actionType來判斷聚焦的行為,聚焦到對應(yīng)的元素
 * @param el
 * @param value
 * @param oldValue
 */
update: function (el,{value,oldValue}) {
 if (value !== oldValue) {
  focusCtrl(el)
 }
},

以上這篇vue-auto-focus: 控制自動聚焦行為的 vue 指令方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI