This article describes the method of JS to determine whether mobile phones and PCs can choose different execution events. Share it for your reference. The details are as follows:
Determine whether it is a mobile phone:
function isMobile(){ var sUserAgent= (), bIsIpad= (/ipad/i) == "ipad", bIsIphoneOs= (/iphone os/i) == "iphone os", bIsMidp= (/midp/i) == "midp", bIsUc7= (/rv:1.2.3.4/i) == "rv:1.2.3.4", bIsUc= (/ucweb/i) == "ucweb", bIsAndroid= (/android/i) == "android", bIsCE= (/windows ce/i) == "windows ce", bIsWM= (/windows mobile/i) == "windows mobile", bIsWebview = (/webview/i) == "webview"; return (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM); }
To determine which event to use:
var touchStart,touchMove,touchEnd; touchStart = isMobile() ? 'touchstart' : 'mousedown'; touchMove = isMobile() ? 'touchmove' : 'mousemove'; touchEnd = isMobile() ? 'touchend' : 'mouseup';
The corresponding handling of three events:
touchstart:function(e){ var e=e || ; //To determine which event to use stopDefault(e); //Different browsers have different default events methods to block browsers if(isMobile()){ //If it's a mobile phone var touch=[0]; this.y1= }else{ this.y1=; //If it's not a mobile phone } this.y2=0; }, touchmove:function(e){ var e=e || ; stopDefault(e); if(isMobile()){ var touch=[0]; this.y2=; }else{ this.y2=; } }, touchend:function(e){ var e=e || ; stopDefault(e); if(this.y2==0){ return; } var diffY=this.y2-this.y1; if(diffY>50){ (); }else if(diffY<-50){ (); } this.y1=0, this.y2=0; },
Block the browser's default event method:
function stopDefault(e){ var e=e || ; if(){ (); }else{ =false; } }
I hope this article will be helpful to everyone's JavaScript programming.