The example of this article tells JS to judge the mobile phone and the PC side to choose different execution event methods. Share to everyone for your reference. Specifically as follows:
To determine whether a cell phone:
function IsMobile () {
var suseragent= navigator.userAgent.toLowerCase (),
bisipad= Suseragent.match (/ipad/i) = "ipad",
bisiphoneos= suseragent.match (/iphone os/i) = "iphone OS",
bismidp= suseragent.match (/midp/i) = " MIDP ",
bisuc7= suseragent.match (/rv:1.2.3.4/i) = =" rv:1.2.3.4 ",
bisuc= suseragent.match (/ucweb/i) =" UCWeb " ,
bisandroid= suseragent.match (/android/i) = = "Android",
bisce= suseragent.match (/windows ce/i) = = "Windows CE ",
biswm= suseragent.match (/windows mobile/i) = =" Windows Mobile ",
Biswebview = Suseragent.match (/webview/i) = = "WebView";
Return (Bisipad | | bisiphoneos | | bismidp | | bIsUc7 | bisuc | | bisandroid | | bisce | | biswm);
}
To judge the use of that kind of event:
var touchstart,touchmove,touchend;
Touchstart = IsMobile ()? ' Touchstart ': ' MouseDown ';
Touchmove = IsMobile ()? ' Touchmove ': ' MouseMove ';
Touchend = IsMobile ()? ' Touchend ': ' MouseUp ';
The corresponding processing of three kinds of events:
Touchstart:function (e) {
var e=e | | window.event;//To determine which event Stopdefault to use
(e); Different browsers, block browser default event method different
if (IsMobile ()) { //if is mobile phone
var touch=e.touches[0];
This.y1=touch.pagey
}else{
this.y1=e.pagey; If not cell phone
}
this.y2=0
,
touchmove:function (e) {
var e=e | | window.event;
Stopdefault (e);
if (IsMobile ()) {
var touch=e.touches[0];
This.y2=touch.pagey;
} else{
this.y2=e.pagey
}
},
touchend:function (e) {
var e=e | | window.event;
Stopdefault (e);
if (this.y2==0) {return
;
}
var diffy=this.y2-this.y1;
if (diffy>50) {
this.donext ();
} else if (diffy<-50) {
this.doprev ();
}
This.y1=0,
this.y2=0;
},
To block the browser default event method:
function Stopdefault (e) {
var e=e | | window.event;
if (e.preventdefault) {
e.preventdefault ();
} else{
e.returnvalue=false
}
}
I hope this article will help you with your JavaScript programming.