JS to judge the mobile phone and PC side choose different Execution event method _javascript skill

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.