欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何在Vue里建立長按指令

 更新時間:2018年08月20日 09:53:08   作者:_小生_  
這篇文章主要介紹了詳解如何在Vue里建立長按指令,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

您是否曾想過按住按鈕幾秒鐘才能在Vue應(yīng)用程序中執(zhí)行某個功能?

您是否曾想在應(yīng)用程序上創(chuàng)建一個按鈕,通過按一次(或按住按鈕的整個輸入)來清除單個輸入?

如果你曾有過這些想法,很好,我也是。那么恭喜你看到了這篇文章。

本文將解釋如何通過按下(或按?。┌粹o來執(zhí)行功能和刪除輸入。

首先,我將解釋如何在VanillaJS中實現(xiàn)這一目標。然后,為它創(chuàng)建一個Vue指令。

那么,讓我們開始吧。

原理

為了實現(xiàn)長按,用戶需要按住按鈕幾秒鐘。

要在代碼中復(fù)制它,我們需要在按下鼠標“單擊”按鈕時監(jiān)聽,啟動計時器,不管我們希望用戶在執(zhí)行函數(shù)之前按住按鈕,并在時間設(shè)置之后執(zhí)行該功能。

非常簡單!但是,我們需要知道用戶何時按住該按鈕。

怎么做

當用戶單擊按鈕時,在單擊事件之前會觸發(fā)另外兩個事件: mousedown 和 mouseup 。

當用戶按下鼠標按鈕時會調(diào)用 mousedown 事件,而當用戶釋放該按鈕時會調(diào)用mouseup事件。

我們需要做的就是:

發(fā)生mousedown事件后啟動計時器。

清除該計時器,并且在2secs標記之前觸發(fā)mouseup事件后不執(zhí)行該函數(shù)。即完整點擊事件。

只要計時器在到達那個時間之前沒有被清除,我們就會發(fā)現(xiàn)mouseup事件沒有被觸發(fā) - 我們可以說用戶沒有釋放按鈕。因此,它被認為是長按,然后我們可以繼續(xù)執(zhí)行所述功能。

實際操作

讓我們深入研究代碼并完成這項工作。

首先,我們必須定義3件事,即:

variable 用于存儲計時器。

start 函數(shù)啟動計時器。

cancel 函數(shù)取消定時器

變量

這個變量基本上保存了setTimeout的值,所以我們可以在發(fā)生mouseup事件時取消它。

let pressTimer = null;

我們將變量設(shè)置為null,這樣我們就可以檢查變量,以便知道當前是否有一個活動定時器,然后才能取消它。

啟動功能

該函數(shù)由setTimeout組成,它基本上是Javascript中的一種方法,它允許我們在函數(shù)中聲明的特定持續(xù)時間之后執(zhí)行函數(shù)。

請記住,在創(chuàng)建click事件的過程中,會觸發(fā)兩個事件。但我們需要啟動計時器的是mousedown事件。因此,如果是單擊事件,我們不需要啟動計時器。

// Create timeout ( run function after 1s )
let start = (e) => {
  
  // Make sure the event trigger isn't a click event
  if (e.type === 'click' && e.button !== 0) {
    return;
  }
  // Make sure we don't currently have a setTimeout running
  // before starting another
  if (pressTimer === null) {
    pressTimer = setTimeout(() => {
      // Execute soemthing !!!
    }, 1000)
  }
}

取消功能

這個函數(shù)基本上就是名字所說的,取消了調(diào)用start函數(shù)時創(chuàng)建的setTimeout。

要取消setTimeout,我們將在javascript中使用 clearTimeout 方法,該方法基本上清除了使用setTimeout()設(shè)置的計時器方法。

在使用clearTimeout之前,我們首先需要檢查 pressTimer 變量是否設(shè)置為null。如果它未設(shè)置為null,則表示存在活動計時器。所以,我們需要清除計時器,你猜對了,將 pressTimer 變量設(shè)置為 null 。

let cancel = (e) => {
  // Check if timer has a value or not
  if (pressTimer !== null) {
    clearTimeout(pressTimer)
    pressTimer = null
  }
}

一旦 mouseup 事件被觸發(fā),就會調(diào)用此函數(shù)。

設(shè)置觸發(fā)器

剩下的就是將事件監(jiān)聽器添加到要添加長按效果的按鈕上。

addEventListener("mousedown", start);
addEventListener("click", cancel);

總而言之,我們有:

// Define variable
let pressTimer = null;

// Create timeout ( run function after 1s )
let start = (e) => {

  if (e.type === 'click' && e.button !== 0) {
    return;
  }

  if (pressTimer === null) {
    pressTimer = setTimeout(() => {

      // Execute something !!!

    }, 1000);
  }
}

// Cancel Timeout
let cancel = (e) => {

  // Check if timer has a value or not
  if (pressTimer !== null) {
    clearTimeout(pressTimer);
    pressTimer = null;
  }
}

// select element with id longPressButton
let el = document.getElementById('longPressButton');

// Add Event listeners
el.addEventListener("mousedown", start);

// Cancel timeouts if this events happen
el.addEventListener("click", cancel);
el.addEventListener("mouseout", cancel);

將它全部包裝在Vue指令中

在創(chuàng)建Vue指令時,Vue允許我們在組件的全局或本地定義指令,但在本文中我們將使用全局路由。

讓我們構(gòu)建完成此任務(wù)的指令。

首先,我們必須聲明自定義指令的名稱。

Vue.directive('longpress', {
 
}

這基本上注冊了一個名為 v-longpress的全局自定義指令.

接下來,我們使用一些參數(shù)添加bind hook函數(shù) ,這允許我們引用元素指令綁定,獲取傳遞給指令的值并標識使用該指令的組件。

Vue.directive('longpress', {
 bind: function (el, binding, vNode) {
  
 }
}

接下來,我們在bind函數(shù)中添加我們的長按javascript代碼。

Vue.directive('longpress', {
  bind: function (el, binding, vNode) {

    // Define variable
    let pressTimer = null

    // Define funtion handlers
    // Create timeout ( run function after 1s )
    let start = (e) => {

      if (e.type === 'click' && e.button !== 0) {
        return;
      }

      if (pressTimer === null) {
        pressTimer = setTimeout(() => {
          // Execute something !!!
        }, 1000)
      }
    }

    // Cancel Timeout
    let cancel = (e) => {
      // Check if timer has a value or not
      if (pressTimer !== null) {
        clearTimeout(pressTimer)
        pressTimer = null
      }
    }

    // Add Event listeners
    el.addEventListener("mousedown", start);
    // Cancel timeouts if this events happen
    el.addEventListener("click", cancel);
    el.addEventListener("mouseout", cancel);
  }
})

接下來,我們需要添加一個函數(shù)來運行將傳遞給 longpress 指令的方法。

// Long Press vue directive
Vue.directive('longpress', {
  bind: function (el, binding, vNode) {

    // Define variable
    let pressTimer = null

    // Define funtion handlers
    // Create timeout ( run function after 1s )
    let start = (e) => {

      if (e.type === 'click' && e.button !== 0) {
        return;
      }

      if (pressTimer === null) {
        pressTimer = setTimeout(() => {
          // Execute function
          handler()
        }, 1000)
      }
    }

    // Cancel Timeout
    let cancel = (e) => {
      // Check if timer has a value or not
      if (pressTimer !== null) {
        clearTimeout(pressTimer)
        pressTimer = null
      }
    }
    // Run Function
    const handler = (e) => {
      // Execute method that is passed to the directive
      binding.value(e)
    }

    // Add Event listeners
    el.addEventListener("mousedown", start);

    // Cancel timeouts if this events happen
    el.addEventListener("click", cancel);
    el.addEventListener("mouseout", cancel);
    
  }
})

現(xiàn)在我們可以在我們的Vue應(yīng)用程序中使用該指令,該指令將正常工作,直到用戶添加的值不是指令值中的函數(shù)。所以我們必須通過在發(fā)生這種情況時警告用戶來防止這種情況。

要警告用戶,我們將以下內(nèi)容添加到bind函數(shù):

// Make sure expression provided is a function
if (typeof binding.value !== 'function') {
 // Fetch name of component
 const compName = vNode.context.name
 // pass warning to console
 let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be`
 if (compName) { warn += `Found in component '${compName}' ` }
 console.warn(warn)
}

最后,這個指令也適用于觸控設(shè)備。所以我們?yōu)?touchstart , touchend & touchcancel 添加事件監(jiān)聽器。

把所有東西放在一起:

Vue.directive('longpress', {
  bind: function (el, binding, vNode) {
    // Make sure expression provided is a function
    if (typeof binding.value !== 'function') {
      // Fetch name of component
      const compName = vNode.context.name
      // pass warning to console
      let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be`
      if (compName) { warn += `Found in component '${compName}' ` }

      console.warn(warn)
    }

    // Define variable
    let pressTimer = null

    // Define funtion handlers
    // Create timeout ( run function after 1s )
    let start = (e) => {

      if (e.type === 'click' && e.button !== 0) {
        return;
      }

      if (pressTimer === null) {
        pressTimer = setTimeout(() => {
          // Run function
          handler()
        }, 1000)
      }
    }

    // Cancel Timeout
    let cancel = (e) => {
      // Check if timer has a value or not
      if (pressTimer !== null) {
        clearTimeout(pressTimer)
        pressTimer = null
      }
    }
    // Run Function
    const handler = (e) => {
      binding.value(e)
    }

    // Add Event listeners
    el.addEventListener("mousedown", start);
    el.addEventListener("touchstart", start);
    // Cancel timeouts if this events happen
    el.addEventListener("click", cancel);
    el.addEventListener("mouseout", cancel);
    el.addEventListener("touchend", cancel);
    el.addEventListener("touchcancel", cancel);
  }
})

現(xiàn)在引用我們的Vue組件:

<template>
  <div>
    <button v-longpress="incrementPlusTen" @click="incrementPlusOne">{{value}}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: 10
    }
  },
  methods: {
    // Increment value plus one
    incrementPlusOne() {
      this.value++
    },
    // increment value plus 10
    incrementPlusTen() {
      this.value += 10
    }
  }
}
</script>

如果您希望了解有關(guān)自定義指令的更多信息,可以使用的鉤子函數(shù),可以傳遞給此鉤子函數(shù)的參數(shù),函數(shù)縮寫。偉大的家伙@vuejs在解釋它這里方面做得很好。

成功 !!!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue頁面鎖屏的完美解決方法記錄

    vue頁面鎖屏的完美解決方法記錄

    鎖屏是指用戶暫時離開時點擊鎖屏,用戶無法跳轉(zhuǎn)到其他頁面,回來輸入密碼點擊解鎖,下面這篇文章主要給大家介紹了關(guān)于vue頁面鎖屏的完美解決方法,需要的朋友可以參考下
    2022-06-06
  • Vue3+Vite實現(xiàn)動態(tài)路由的詳細實例代碼

    Vue3+Vite實現(xiàn)動態(tài)路由的詳細實例代碼

    我們在開發(fā)大型系統(tǒng)的時候一般都需要動態(tài)添加路由,下面這篇文章主要給大家介紹了關(guān)于Vue3+Vite實現(xiàn)動態(tài)路由的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • Vue實現(xiàn)內(nèi)部組件輪播切換效果的示例代碼

    Vue實現(xiàn)內(nèi)部組件輪播切換效果的示例代碼

    這篇文章主要介紹了Vue實現(xiàn)內(nèi)部組件輪播切換效果的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 如何解決Element-ui的el-table固定列后出現(xiàn)的表格錯位問題

    如何解決Element-ui的el-table固定列后出現(xiàn)的表格錯位問題

    這篇文章主要介紹了如何解決Element-ui的el-table固定列后出現(xiàn)的表格錯位問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • vue打包使用Nginx代理解決跨域問題

    vue打包使用Nginx代理解決跨域問題

    這篇文章主要介紹了vue打包使用Nginx代理解決跨域問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • vue 取出v-for循環(huán)中的index值實例

    vue 取出v-for循環(huán)中的index值實例

    今天小編就為大家分享一篇vue 取出v-for循環(huán)中的index值實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vuejs簡單驗證碼功能完整示例

    vuejs簡單驗證碼功能完整示例

    這篇文章主要介紹了vuejs簡單驗證碼功能,結(jié)合完整實例形式分析了vue.js驗證碼的生成、顯示、校驗等相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01
  • 前端VUE雙語實現(xiàn)方案詳細教程

    前端VUE雙語實現(xiàn)方案詳細教程

    在項目需求中我們會遇到國際化的中英文切換,這篇文章主要給大家介紹了關(guān)于前端VUE雙語實現(xiàn)方案的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • vue實現(xiàn)全選組件封裝實例詳解

    vue實現(xiàn)全選組件封裝實例詳解

    這篇文章主要介紹了vue?全選組件封裝,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • vue中前端如何實現(xiàn)pdf預(yù)覽功能(含vue-pdf插件用法)

    vue中前端如何實現(xiàn)pdf預(yù)覽功能(含vue-pdf插件用法)

    這篇文章主要給大家介紹了vue中前端如何實現(xiàn)pdf預(yù)覽功能的相關(guān)資料,文中包含vue-pdf插件用法,在前端開發(fā)中,很多時候我們需要進行pdf文件的預(yù)覽操作,需要的朋友可以參考下
    2023-07-07

最新評論