jquery调整窗口大小和移动检测 [英] jquery resize window and mobile detect

查看:71
本文介绍了jquery调整窗口大小和移动检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何同时使用移动侦测和调整窗口大小?例如我有这段代码

How to use mobile detect and window resize at the same time ? For example I have this bit of code

if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) || $(window).width() < 480 ){
    //do this
    }else{
    //do something else
}

我想写一些与上面类似的东西,但检查浏览器是否是移动的,或者窗口是否调整大小加上窗口宽度小于 480 像素.例如将下面的代码与上面的代码结合起来.

and I want to write something similar to the above but check if the browser is mobile or if window is resized plus window width is less than 480 pixel. e.g. to combine the below code with the above code.

 $(window).resize(function() {
      if ($(window).width() < 480) {
         }else{}
 });

推荐答案

如果我正确理解了这个问题,你应该能够简单地这样做:

If I understand the question correctly you should be able to simply do this:

$(window).resize(function() {
    if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) || $(window).width() < 480 ){
    //do this
    }else{
    //do something else
    }
}

或者,您可以在 dom 初始化之前将用户代理存储在一个变量中:

Or, you could store the user agent in a variable before dom initialization:

var isMobile = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
$(document).ready(function() {
     $(window).resize(function() {
         if ($(window).width() < 480 || isMobile) {
         // handle less than 480
         }else{
         //handle else
         }
     });
});

这篇关于jquery调整窗口大小和移动检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆