我们需要在元素上绑定触摸开始和触摸接触,通过计算判断元素是往哪个方向运动
<template>
<view class="content">
<view class=""@touchstart="start" @touchend="end" style="width:100%;height: 20vh;background: #007AFF;">
adasdasd
</view>
</view>
</template>
<script>
export default {
data() {
return {
startData:{clientX:'',clientY:''}
}
},
onLoad() {
},
methods: {
start(e){
this.startData.clientX=e.changedTouches[0].clientX;
this.startData.clientY=e.changedTouches[0].clientY;
},
end(e){
const subX=e.changedTouches[0].clientX-this.startData.clientX;
const subY=e.changedTouches[0].clientY - this.startData.clientY;
if(subY>50 || subY<-50){
if(subY>50){
console.log('下滑')
}else if(subY<-50){
console.log('上滑')
}
}else{
if(subX>100){
console.log('右滑')
}else if(subX<-100){
console.log('左滑')
}else{
console.log('无效')
}
}
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
page{
background: #2C405A;
}
</style>