vue長(zhǎng)按事件和點(diǎn)擊事件沖突的解決
長(zhǎng)按事件和點(diǎn)擊事件沖突的解決
使用場(chǎng)景
在使用vue做移動(dòng)端開(kāi)發(fā)時(shí),遇到了長(zhǎng)按事件和點(diǎn)擊事件沖突的問(wèn)題。
具體需求
點(diǎn)擊標(biāo)簽時(shí)選中標(biāo)簽,再次點(diǎn)擊時(shí)取消選擇,因標(biāo)簽名稱過(guò)長(zhǎng),長(zhǎng)按標(biāo)簽使用tooltip顯示完整標(biāo)簽名稱。
代碼說(shuō)明
長(zhǎng)按是用touchstart事件和touchend事件來(lái)實(shí)現(xiàn)的。在touchstart事件里使用setTimeout,時(shí)間設(shè)置為長(zhǎng)按生效的時(shí)間就可以了,下面上代碼。
HTML部分
關(guān)于這里的.prevent修飾符,是用來(lái)阻止默認(rèn)動(dòng)作。但這里我試過(guò),不加在電腦端運(yùn)行時(shí)會(huì)有異常,手機(jī)端沒(méi)有影響,最好還是加上吧。
? ? <div v-for="(item, index) in List" class="flex a-center j-center" > ? ? ? ? ? ?<van-tag ? v-if="!item.selected" ? ? ? ? ? ?@touchstart.prevent="goTouch(item.name)" ? ? ? ? ? ? ?@touchend.prevent="outTouch(index)" ? ? ? ? ? ? > ? ? ? ? ? ? <span >{{ item.name }}</span> ? ? ? ? ? ? </van-tag> ? ? ? ?? ? ? ? ? ? ? <van-tag v-if="item.selected" ? color="blue"? ? ? ? ? ? ? ?@touchstart.prevent="goTouch(item.name)" ? ? ? ? ? ? ?@touchend.prevent="outTouch(index)"? ? ? ? ? ? ? ?> ? ? ? ? ? ? <span>{{ item.name }}</span> ? ? ? ? ? ? </van-tag> ? ? </div>
JS部分
當(dāng)點(diǎn)擊標(biāo)簽時(shí),timer的值不為0,執(zhí)行單次點(diǎn)擊事件,長(zhǎng)按一秒時(shí)將timer設(shè)置為0,則只執(zhí)行長(zhǎng)按事件。這樣就解決了長(zhǎng)按事件和點(diǎn)擊事件的沖突。
data(){ ?return{ ?timer:0 ?} }, methods:{ //touchstart 事件 goTouch(v) { ? ? ? this.timer = setTimeout(() => { ? ? ? ? this.timer = 0 ? ? ? ? //執(zhí)行長(zhǎng)按事件 ? ? ? }, 1000); ? ? ? return false ?}, ? ?//touchend事件 ? ? outTouch(index) { ? ? ? clearTimeout(this.timer); ? ? ? if(this.timer!=0){ ? ? ? //執(zhí)行單次點(diǎn)擊事件 ? ? ? } ? ? ? return false ?} }
vue web端長(zhǎng)按事件,解決和click沖突
? <div ? ? ? ? ? class="main_content" ? ? ? ? ? @mousedown="loopZoom()" ? ? ? ? ? @mouseup="clearLoopZoom()" ? ? ? ? ? @click="handlerZoom()" ? ? ? ? > ? ? ? ? ? 測(cè)試長(zhǎng)按 ? ? ? ? </div>
? ?handlerZoom() { ? ? ? ? if (this.flag) { ? ? ? ? ? console.log('執(zhí)行click事件') ? ? ? ? } ? ? ? ? this.flag = false ? ? }, ? ? loopZoom() { ? ? ? console.log("長(zhǎng)按開(kāi)始咯") ? ? ? this.firstTime = new Date().getTime() ? ? ? this.timeOut = setTimeout(() => { ? ? ? console.log("長(zhǎng)按事件") ? ? ? }, 800); ? ? }, ? ? clearLoopZoom() { ? ? ? console.log("長(zhǎng)按結(jié)束咯") ? ? ? this.lastTime=new Date().getTime() ? ? ? if (this.lastTime - this.firstTime < 100) { ? ? ? ? ? this.flag=true ? ? ? ? } ? ? ? clearTimeout(this.timeOut); ? ? ? this.timeOut = ""; ? ? ? clearInterval(this.setIntervals); ? ? ? this.setIntervals = ""; ? ? },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue?前端路由工作原理hash與history的區(qū)別
這篇文章主要介紹了Vue?前端路由工作原理hash與history的區(qū)別,文章圍繞主題展開(kāi)vue-router的工作原理的簡(jiǎn)單介紹,感興趣的朋友可以學(xué)習(xí)一下2022-07-07vue-router如何實(shí)時(shí)動(dòng)態(tài)替換路由參數(shù)(地址欄參數(shù))
這篇文章主要介紹了vue-router如何實(shí)時(shí)動(dòng)態(tài)替換路由參數(shù)(地址欄參數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue elementui簡(jiǎn)易側(cè)拉欄的使用小結(jié)
這篇文章主要介紹了vue elementui簡(jiǎn)易側(cè)拉欄的使用,增加了側(cè)拉欄,目的是可以選擇多條數(shù)據(jù)展示數(shù)據(jù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06Vue實(shí)現(xiàn)封裝一個(gè)切片上傳組件
平時(shí)業(yè)務(wù)開(kāi)發(fā)中用el-upload能滿足大部分場(chǎng)景,但是對(duì)于一些大文件的上傳時(shí)會(huì)比較慢,所以自己基于el-upload封裝了一個(gè)切片上傳組件,希望對(duì)大家有所幫助2023-03-03vue實(shí)現(xiàn)web前端登錄頁(yè)數(shù)字驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)web前端登錄頁(yè)數(shù)字驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06基于Vue.js與WordPress Rest API構(gòu)建單頁(yè)應(yīng)用詳解
這篇文章主要介紹了基于Vue.js與WordPress Rest API構(gòu)建單頁(yè)應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09