js判断手机和pc端选择不同执行事件的方法
作者:bea
本文实例讲述了js判断手机和pc端选择不同执行事件的方法。分享给大家供大家参考。具体如下: 判断是否为手机: function isMobile(){ var sUserAgent= navigator.userAgent.toLowerCase(), bIsIpad= sUserAgent.match(/ipad/i) == "ipad", bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os", bIsMi
本文实例讲述了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程序设计有所帮助。
有用 | 无用
判断是否为手机:
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程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 浅谈jquery回调函数callback的使用
- jQuery修改class属性和CSS样式整理
- javascript中AJAX用法实例分析
- JavaScript基础函数整理汇总
- JS解析XML实例分析
- javascript中键盘事件用法实例分析
- javascript中cookie对象用法实例分析
- javascript事件模型实例分析
- JS是按值传递还是按引用传递
- js实现鼠标悬浮给图片加边框的方法
- js控制输入框获得和失去焦点时状态显示的方法
- 使用mouse事件实现简单的鼠标经过特效
- js实现屏幕自适应局部代码分享
- jQuery弹出框代码封装DialogHelper
- jquery中animate的stop()方法作用实例分析
- javascript中sort()的用法实例分析
- JavaScript中的值是按值传递还是按引用传递问题探讨
- javascript结合fileReader 实现上传图片
- 使用JavaScript+canvas实现图片裁剪