方法: 这里使用了jq所以直接用了,大家可以改成原生写法
/**
* 将touch事件改为点击事件
* @param {[string]} dom [css选择器]
* @param {[bool]} derect [方向 x为false,y方向为true]
* @param {Function} callback [要执行的函数]
* @return {[underfined]} [description]
*/
function pt_TouchToClick(dom,derect,callback){
$("body").on('touchstart',dom,function(event){
let x=0,x1=0,flag=0,atrsy=!derect?"pageX":"pageY",e = event || window.event;
x=e.targetTouches[0][atrsy];
$(this).on("touchend",function(event){
let e = event || window.event;
x1=e.changedTouches[0][atrsy],flag=x!=x1?0:1;
})
setTimeout(()=>{ flag && callback.call(this);})
})
}
使用:
pt_TouchToClick(".pt_picture_box #head .right ul li",0,function(){
//做一些事情
})
主要解决了touchstart替代点击事件,滑动也触发的bug
click事件无法触发解决方案:body *
$("body *").on('click', '.pt_picture_box #head .right ul li', function(event) {
//处理
});