场景:在开发APP端时,常用的一个touch事件,在PC端如何将其转换?
措施:代码如下:
var pcFlag=true; //pc端的touch事件转换成mouse事件 var touchEvents = { touchstart:"touchstart", touchmove:"touchmove", touchend:"touchend", initTouchEvents:function () { var self = this; if (self.isPC()) { self.touchstart = "mousedown"; self.touchmove = "mousemove"; self.touchend = "mouseup"; } }, isPC:function(){ //判断pc端与移动端 var userAgentInfo = navigator.userAgent; var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"); //判断用户代理头信息 for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) != -1) { pcFlag = false; break; } } return pcFlag; //true为pc端,false为非pc端 } }; touchEvents.initTouchEvents();
绑定事件的时候,将原来的on('touchstart',function(){}),替换成on('touchEvents.touchstart',function(){}).