国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js判斷手機和pc端選擇不同執行事件的方法

2019-11-20 13:16:40
字體:
來源:轉載
供稿:網友

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

判斷是否為手機:

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';

三種事件的相應處理:

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

阻止瀏覽器默認事件方法:

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

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰浩特市| 临澧县| 伊金霍洛旗| 二连浩特市| 诸暨市| 云龙县| 屏东市| 昌邑市| 米易县| 萨嘎县| 勐海县| 旌德县| 湘乡市| 广昌县| 和田市| 江达县| 安化县| 黄龙县| 新津县| 泸定县| 五台县| 邻水| 万荣县| 綦江县| 许昌县| 芦山县| 荣成市| 内黄县| 资溪县| 宿松县| 太保市| 鹿泉市| 玛纳斯县| 琼海市| 名山县| 武强县| 吉林市| 浠水县| 高清| 涟水县| 定结县|