淘宝小程序判断手势滑动方向
程序员文章站
2024-03-24 20:59:58
...
axml部分
<view style="width:50px;height:50px;background-color:orangered" onTouchMove="onchange" onTouchEnd="onchange" animation="{{animation}}">1</view>
js部分
onchange(event) {
// console.log(event)
// this.animation = my.createAnimation()
this.animation = my.createAnimation({
transformOrigin: "top right",
// transformOrigin:'left right',//元素变化时围绕哪个点进行变换
duration: 100,//动画持续时间
timeFunction: "ease",//定义动画效果 默认linear,有效值:'linear','ease','ease-in','ease-in-out','ease-out','step-start','step-end'
delay: 0,//动画延迟时间
})
this.animation.translate(event.changedTouches[0].clientX, event.changedTouches[0].clientY).step()
this.setData({ animation: this.animation.export() })
//判断手势类型
if (event.type == "touchEnd") {
//判断手势滑动方向
if (event.changedTouches[0].clientX > this.data.deltaX) {
console.log("右滑")
}else if(event.changedTouches[0].clientX < this.data.deltaX){
console.log("左滑")
}
this.setData({
//初始状态 deltaX=0
deltaX: event.changedTouches[0].clientX
})
}
}