Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

javascript - 如何使用JavaScript检测屏幕分辨率?(How to detect the screen resolution with JavaScript?)

有没有一种方法可以适用于所有浏览器?

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

original answer

(原始答案)

Yes.

(是。)

window.screen.availHeight
window.screen.availWidth

update 2017-11-10

(更新 2017-11-10)

From Tsunamis in the comments:

(来自海啸的评论:)

To get the native resolution of ie a mobile device you have to multiply with the device pixel ratio: window.screen.width * window.devicePixelRatio and window.screen.height * window.devicePixelRatio .

(要获得移动设备的原始分辨率,您必须乘以设备像素比率: window.screen.width * window.devicePixelRatiowindow.screen.height * window.devicePixelRatio 。)

This will also work on desktops, which will have a ratio of 1.

(这也将在台式机上工作,该台式机的比率为1。)

And from Ben in another answer:

(而来自的另一个答案是:)

In vanilla JavaScript, this will give you the AVAILABLE width/height:

(在普通JavaScript中,这将为您提供可用的宽度/高度:)

 window.screen.availHeight window.screen.availWidth 

For the absolute width/height, use:

(对于绝对宽度/高度,请使用:)

 window.screen.height window.screen.width 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...