[英]How can I stop a JS function from running on a mobile device?
I have the following code running and I need to make it stop if you are using a mobile device 我运行了以下代码,如果您使用移动设备,我需要停止它
jQuery(document).ready(function($){
var inHeight = $("#wrapper").innerHeight();
$("#wrapper .col").each(function(){
$(this).height(inHeight+"px");
$(this).find('.content').height((inHeight-60)+"px");
});
});
Can I use something like if($(window).width()<600){ /* do something */ }
If so what shall I write between the curved brackets? 我可以使用
if($(window).width()<600){ /* do something */ }
东西if($(window).width()<600){ /* do something */ }
如果是这样的话我应该在弯曲的括号之间写些什么?
Thank you! 谢谢!
You can try: 你可以试试:
if(!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent) )) {
jQuery(document).ready(function($){
var inHeight = $("#wrapper").innerHeight();
$("#wrapper .col").each(function(){
$(this).height(inHeight+"px");
$(this).find('.content').height((inHeight-60)+"px");
});
});
}
So if it is not a mobile device then you run the code 因此,如果它不是移动设备,那么您运行代码
Using $(window).width
it is not a very good solution. 使用
$(window).width
这不是一个很好的解决方案。 Think what will happen if i am not using a mobile device and just change dimensions in my browser window 想想如果我不使用移动设备而只是在浏览器窗口中更改尺寸会发生什么
Try this : 试试这个 :
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if( isMobile.any() ) {....} else { // place your code here }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.