vue實(shí)現(xiàn)按鈕的長按功能
先給大家介紹下vue實(shí)現(xiàn)按鈕的長按功能,效果圖如下:
實(shí)現(xiàn)效果圖:

實(shí)現(xiàn)思路:
給需要操作的 dom 元素添加touchstart(點(diǎn)擊開始)、touchend(點(diǎn)擊結(jié)束)、touchmove(手指移動(dòng))事件
在使用touchstart(點(diǎn)擊開始)事件的時(shí)候設(shè)置定時(shí)器,在指定時(shí)間內(nèi)如果不做其他操作就視為 長按事件,執(zhí)行 長按事件 的同時(shí)需要設(shè)定當(dāng)前不是 單擊事件,以防止touchend(點(diǎn)擊結(jié)束)執(zhí)行的時(shí)候同時(shí)出現(xiàn) 長按事件 和 單擊事件
在 touchend(點(diǎn)擊結(jié)束)里面清除定時(shí)器,并判斷是不是 單擊事件
在 touchmove(手指移動(dòng))里面清除定時(shí)器,并設(shè)定當(dāng)前不是 單擊事件
代碼
HTML
<div @touchstart="gotouchstart" @touchmove="gotouchmove" @touchend="gotouchend" class="div">按鈕</div>
JS
export default {
data() {
return {}
},
methods: {
// 手指按下事件
gotouchstart() {
window.isClick = true
clearTimeout(this.timeOut)
this.timeOut = setTimeout(function() {
console.log('在這里執(zhí)行長按事件')
window.isClick = false
}, 500)
},
//手釋放,如果在500毫秒內(nèi)就釋放,則取消長按事件,此時(shí)可以執(zhí)行onclick應(yīng)該執(zhí)行的事件
gotouchend() {
if (window.isClick) {
console.log('在這里執(zhí)行點(diǎn)擊事件')
}
//如果手指有移動(dòng),則取消所有事件,此時(shí)說明用戶只是要移動(dòng)而不是長按
gotouchmove() {
console.log('手指移動(dòng)了')
window.isClick = false
}
// 項(xiàng)目銷毀前清除定時(shí)器
beforeDestroy() {
clearTimeout(this.timeOut)
}
}style(去除長按出現(xiàn)的文字復(fù)制影響)
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;補(bǔ)充:下面看下Vue使用自定義指令實(shí)現(xiàn)按鈕長按提示功能,超簡單!
項(xiàng)目場景
點(diǎn)擊按鈕實(shí)現(xiàn)長按,用戶需要按下按鈕幾秒鐘,然后觸發(fā)相應(yīng)的事件
實(shí)現(xiàn)思路
- 首先,需要?jiǎng)?chuàng)建一個(gè)計(jì)時(shí)器,在2 秒后開始執(zhí)行函數(shù),用戶按下按鈕時(shí)觸發(fā)
mousedown事件,開始計(jì)時(shí); - 當(dāng)鼠標(biāo)移開按鈕時(shí)開始調(diào)用
mouseout事件 - 第一種情況,當(dāng)
mouseup事件 2 秒內(nèi)被觸發(fā)了,需要清除計(jì)時(shí)器,相當(dāng)于進(jìn)行了普通的點(diǎn)擊事件 - 第二種情況,當(dāng)計(jì)時(shí)器沒有在 2 秒內(nèi)清除,那么這就可以判定為一次長按,可以執(zhí)行長按的邏輯了。
- 如果在移動(dòng)端使用,使用的就是
touchstart,touchend事件了 實(shí)現(xiàn)效果

實(shí)現(xiàn)代碼
<template>
<div>
<div class="btn-copy"><el-button v-press="handleClickLong">長按</el-button></div>
</div>
</template>
<script>
import press from '../../directive/test/press'
export default {
directives: {
press
},
data(){
return{
}
},
methods:{
handleClickLong () {
alert('實(shí)現(xiàn)了長按哦?。?!')
},
}
}
</script>
<style lang="scss">
</style>press.js文件如下:
const press = {
bind: function (el, binding, vNode) {
console.log(el, binding, vNode)
// el就是dom
if (typeof binding.value !== 'function') {
throw 'callback must be a function'
}
// 定義變量
let pressTimer = null
// 創(chuàng)建計(jì)時(shí)器( 2秒后執(zhí)行函數(shù) )
let start = (e) => {
if (e.type === 'click' && e.button !== 0) {
return
}
if (pressTimer === null) {
pressTimer = setTimeout(() => {
handler()
}, 2000)
}
}
// 取消計(jì)時(shí)器
let cancel = (e) => {
console.log(e)
if (pressTimer !== null) {
clearTimeout(pressTimer)
pressTimer = null
}
}
// 運(yùn)行函數(shù)
const handler = (e) => {
binding.value(e)
}
// 添加事件監(jiān)聽器
el.addEventListener('mousedown', start)
el.addEventListener('touchstart', start)
// 取消計(jì)時(shí)器
el.addEventListener('click', cancel)
el.addEventListener('mouseout', cancel)
el.addEventListener('touchend', cancel)
el.addEventListener('touchcancel', cancel)
},
// 當(dāng)傳進(jìn)來的值更新的時(shí)候觸發(fā)
componentUpdated(el, { value }) {
el.$value = value
},
// 指令與元素解綁的時(shí)候,移除事件綁定
unbind(el) {
el.removeEventListener('click', el.handler)
},
}
export default press到此這篇關(guān)于vue實(shí)現(xiàn)按鈕的長按功能的文章就介紹到這了,更多相關(guān)vue按鈕長按內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue拖拽組件列表實(shí)現(xiàn)動(dòng)態(tài)頁面配置功能
這篇文章主要介紹了Vue拖拽組件列表實(shí)現(xiàn)動(dòng)態(tài)頁面配置功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
vue之webpack -v報(bào)錯(cuò)解決方案總結(jié)
這篇文章主要介紹了vue之webpack -v報(bào)錯(cuò)解決方案總結(jié),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
利用Vue Router實(shí)現(xiàn)單頁面應(yīng)用(SPA)的代碼示例
在當(dāng)今前端開發(fā)中,單頁面應(yīng)用(SPA)已成為一種主流的開發(fā)模式,它通過在用戶與網(wǎng)頁之間提供更流暢的交互體驗(yàn),來改變傳統(tǒng)多頁面應(yīng)用的思維,本文將詳細(xì)介紹如何利用 Vue.js 中的 Vue Router 來實(shí)現(xiàn)一個(gè)簡單的單頁面應(yīng)用,需要的朋友可以參考下2025-01-01
Vue??vuex配置項(xiàng)和多組件數(shù)據(jù)共享案例分享
這篇文章主要介紹了Vue??vuex配置項(xiàng)和多組件數(shù)據(jù)共享案例分享,文章圍繞Vue?Vuex的相關(guān)資料展開配置項(xiàng)和多組件數(shù)據(jù)共享的案例分享,需要的小伙伴可以參考一下2022-04-04
VUE項(xiàng)目啟動(dòng)沒有問題但代碼中script標(biāo)簽有藍(lán)色波浪線標(biāo)注
這篇文章主要給大家介紹了關(guān)于VUE項(xiàng)目啟動(dòng)沒有問題但代碼中script標(biāo)簽有藍(lán)色波浪線標(biāo)注的相關(guān)資料,文中將遇到的問題以及解決的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
vue 路由守衛(wèi)(導(dǎo)航守衛(wèi))及其具體使用
這篇文章主要介紹了vue 路由守衛(wèi)(導(dǎo)航守衛(wèi))及其具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
vue數(shù)據(jù)響應(yīng)式原理知識點(diǎn)總結(jié)
在本篇文章里小編給各位整理的是一篇關(guān)于vue數(shù)據(jù)響應(yīng)式原理知識點(diǎn)總結(jié),有興趣的朋友們可以跟著學(xué)習(xí)下。2020-02-02
vue中手動(dòng)封裝iconfont組件解析(三種引用方式的封裝和使用)
這篇文章主要介紹了vue中手動(dòng)封裝iconfont組件(三種引用方式的封裝和使用),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

