Javascript 使用 jquery 确定屏幕宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29883329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:13:18  来源:igfitidea点击:

Using jquery to determine screen width

javascriptjquerymobilewidth

提问by dsol828

Having trouble getting this script to run properly on an iphone 6. Keeps coming up as "not mobile". What am I missing?

无法让此脚本在 iphone 6 上正常运行。不断出现“非移动”。我错过了什么?

$(document).ready(function(){
    if ($(window).width < 700){
        alert("mobile");
    }
    else {
        alert("not mobile");
    }
});

EDIT: apologies, the code I typed here had a typo, but was not the cause for my issue. I had inaccurate info on iphone resolution. Thanks everyone!

编辑:抱歉,我在这里输入的代码有错别字,但不是我问题的原因。我有关于 iphone 分辨率的不准确信息。谢谢大家!

回答by ekuusela

The iPhone 6 display has a resolution of 1334x750. When emulating iPhone6 in chrome dev tools the width is reported as 980(I don't know if this is accurate).

iPhone 6 显示屏的分辨率为 1334x750。在 chrome 开发工具中模拟 iPhone6 时,宽度报告为980(我不知道这是否准确)。

You might be interested in this: http://detectmobilebrowsers.com/

您可能对此感兴趣:http: //detectmobilebrowsers.com/

Also, as others have noted, replace $(window).widthwith $(window).width()

此外,正如其他人所指出的,替换$(window).width$(window).width()

回答by Fund Monica's Lawsuit

Well, ignoring what ekuusela saidabout screen resolution, you seem to have forgotten your parentheses after width, which is a method, not a field. To fix this, just add ()after it:

好吧,忽略ekuusela 说的屏幕分辨率,你好像忘记了后面的括号width,是方法,不是字段。要解决此问题,只需在()其后添加:

if ($(window).width() < 700)

See the documentation for width()for more info.

有关更多信息,请参阅文档width()

回答by Sami

iPhone6 screen is 1334x750 pixels. If you only use the width to detect mobile user, see thisinstead.

iPhone6 屏幕为 1334x750 像素。如果您仅使用宽度来检测移动用户,请改为查看此内容

回答by ScottMichaud

JQuery uses $(window).width(). It's a function, not a property.

JQuery 使用$(window).width(). 它是一个函数,而不是一个属性。

回答by Bill Criswell

You would want .width(), not just .width. Also, log it and make sure it's what you're expecting.

你会想要的.width(),不只是.width。此外,记录它并确保它是您所期望的。

回答by MaxRocket

This thread gets deep into the options both in Javascript and JQuery

该线程深入研究了 Javascript 和 JQuery 中的选项

Get the size of the screen, current web page and browser window

获取屏幕大小、当前网页和浏览器窗口