安装npm包:npm i vue3-touch-events
然后在main.js里:
import Vue3TouchEvents from "vue3-touch-events"
const app = createApp(App);
app.use(Vue3TouchEvents);
使用方法如下,有些特殊的情况下需要加一个disable_touch的开关来阻止swipe和press事件的发生。
let disable_touch=false;
async function ClickImg(){
disable_touch=true;
await nextTick();
disable_touch=false;
}
function SwipeImg(type)
{
if(disable_touch) return;
console.log('swipe', type);
}
function PressImg(){ console.log('press image'); }
<img v-touch:press="PressImg" v-touch:swipe.left="SwipeImg('left')" v-touch:swipe.right="SwipeImg('right')" :src="img_src" :alt="img.alt" :title="img.alt" :class="img_class" @click="ClickImg"/>