javascript 检测多台显示器的屏幕宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9407796/
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
Detect screen width for multiple monitors
提问by Pratik Thakkar
My website is optimized (with fixed width) for 1024x768 layout. I plan to add vertical banners on either side of the page for people having resolution width 1280 or higher.
我的网站针对 1024x768 布局进行了优化(具有固定宽度)。我计划为分辨率宽度为 1280 或更高的人在页面的两侧添加垂直横幅。
Using screen.width, I can easily do this. But I face a problem when trying it on machines with multiple monitors.
使用 screen.width,我可以轻松做到这一点。但是在具有多台显示器的机器上尝试时我遇到了问题。
Lets assume the below mentioned scenario:
Monitor 1 (primary display) - Resolution is 1024 x 768
Monitor 2 (secondary display) - Resolution is 1440 x 900
让我们假设下面提到的场景:
显示器 1(主显示器)- 分辨率为 1024 x 768
显示器 2(辅助显示器)- 分辨率为 1440 x 900
screen.width always shows width as 1024 irrespective of the monitor I browse the page on. The values are reversed if I change the primary monitor to Monitor 2.
screen.width 始终将宽度显示为 1024,无论我浏览页面的显示器如何。如果我将主监视器更改为监视器 2,则这些值会反转。
This is a big problem especially for people having 1024x768 resolution as primary resolution. This means that I could potentially loose banner impressions in such scenarios.
这是一个大问题,特别是对于将 1024x768 分辨率作为主要分辨率的人。这意味着在这种情况下我可能会丢失横幅展示次数。
I would really appreciate any help on this.
我真的很感激这方面的任何帮助。
采纳答案by Pratik Thakkar
Thanks for your response duskwuff. Helped me compile the below function which solved the problem for me.
感谢您的回复 duskwuff。帮助我编译了以下功能,为我解决了问题。
function getBrowserWith()
{
if(($.browser.msie == true && $.browser.version == '9.0') || $.browser.webkit == true || $.browser.mozilla == true)
return window.innerWidth;
else if(($.browser.msie == true) && ($.browser.version == '7.0' || $.browser.version == '8.0'))
return document.documentElement.clientWidth;
else
return screen.width;
}
Important notes
重要笔记
- jQuery 1.4+ is required
- Note that the above function has been tested only for IE7+, FF7+ & Chrome16+ browsers.
- 需要 jQuery 1.4+
- 请注意,上述功能仅针对 IE7+、FF7+ 和 Chrome16+ 浏览器进行了测试。
回答by duskwuff -inactive-
Try looking at window.innerWidth
(the width of the web page on screen) instead of window.screen.width
.
尝试查看window.innerWidth
(屏幕上网页的宽度)而不是window.screen.width
。