本文实例讲述了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() "htmlcode">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程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com
暂无“js判断手机和pc端选择不同执行事件的方法”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。