欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js判斷手機(jī)和pc端選擇不同執(zhí)行事件的方法

 更新時(shí)間:2015年01月30日 09:17:45   作者:fonglezen  
這篇文章主要介紹了js判斷手機(jī)和pc端選擇不同執(zhí)行事件的方法,可實(shí)現(xiàn)判斷手機(jī)端還是PC端再選擇對(duì)應(yīng)的執(zhí)行事件的功能,是非常實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了js判斷手機(jī)和pc端選擇不同執(zhí)行事件的方法。分享給大家供大家參考。具體如下:

判斷是否為手機(jī):

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);
}

判斷使用那種事件:

var touchStart,touchMove,touchEnd;
touchStart = isMobile() ? 'touchstart' : 'mousedown';
touchMove = isMobile() ? 'touchmove' : 'mousemove';
touchEnd = isMobile() ? 'touchend' : 'mouseup';

三種事件的相應(yīng)處理:

touchstart:function(e){
 var e=e || window.event; //要判斷使用哪種event
 stopDefault(e);     //不同的瀏覽器,阻止瀏覽器默認(rèn)事件方法不同
 
 if(isMobile()){     //如果是手機(jī)
  var touch=e.touches[0];
  this.y1=touch.pageY
 }else{
  this.y1=e.pageY;   //如果不是手機(jī)
 }
 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;
},

阻止瀏覽器默認(rèn)事件方法:

function stopDefault(e){
  var e=e || window.event;
 if(e.preventDefault){
 e.preventDefault();
 }else{
 e.returnValue=false;
 }
}

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論