js判斷手機(jī)和pc端選擇不同執(zhí)行事件的方法
本文實(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)文章
js驗(yàn)證身份證號(hào)有效性并提示對(duì)應(yīng)信息
這篇文章主要介紹了一段超級(jí)全面的二代身份證號(hào)碼驗(yàn)證程序,2015-10-10
uni-app如何讀取本地json數(shù)據(jù)文件并渲染到頁面上
在做前端開發(fā)的時(shí)候,少不了要用一些模擬的json的數(shù)據(jù)來進(jìn)行測(cè)試,這篇文章主要給大家介紹了關(guān)于uni-app如何讀取本地json數(shù)據(jù)文件并渲染到頁面上的相關(guān)資料,需要的朋友可以參考下2022-08-08
TypeError: Cannot set properties of 
這篇文章主要介紹了TypeError: Cannot set properties of undefined (setting ‘xx‘)的問題,本文給大家分享完美解決方案,需要的朋友可以參考下2023-09-09
Javascript和jquery在selenium的使用過程
這篇文章主要介紹了Javascript和jquery在selenium的使用過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
2007/12/23更新創(chuàng)意無限,簡單實(shí)用(javascript log)
在javascript開發(fā)過程中,如果總是使用alert的方式調(diào)試程序,在某些簡單的程序中是可行的. 但是在通常的項(xiàng)目很復(fù)雜,這種方式已經(jīng)很難滿足,企業(yè)級(jí)開發(fā)的需要。2007-12-12
判斷目標(biāo)是否是window,document,和擁有tagName的Element的代碼
判斷目標(biāo)是否是window,document,和擁有tagName的Element的代碼,需要的朋友可以參考下。2010-05-05
JS實(shí)現(xiàn)簡單的頂部定時(shí)關(guān)閉層效果
這篇文章主要介紹了通過JS實(shí)現(xiàn)的簡單頂部定時(shí)關(guān)閉層效果,需要的朋友可以參考下2014-06-06

