簡體   English   中英

獲取Javascript中瀏覽器的最大像素寬度

[英]Get the maximum pixel width of the browser in Javascript

我有一台1920x1080的顯示器。 我的Windows GUI縮放比例為125%。 這意味着要使網站看起來像正常的100%縮放比例,我需要將Chrome縮放到80%(80%* 125%= 1)。 (因此,在這種情況下,devicePixelRatio為1。)

我想獲得瀏覽器窗口的最大大小(以像素為單位),也就是說,如果最大化,大小將是多少。

有人認為window.screen.width可以做到,但是在Chrome中,我得到的是1536,而不是1920。(1536是1920 / 1.25)

我提出了三種解決方案:一種用於Chrome瀏覽器,一種用於Edge,另一種用於Firefox。 我現在要弄清楚的唯一一件事是如何將解決方案同步到一個:

鉻:

let adj = (window.screen.width == window.outerWidth) ? 0 : -15
let maxScreenWidth = document.body.getBoundingClientRect().width * (window.screen.width / (window.outerWidth + adj))

邊緣:

// Edit: simply "window.screen.width" also works here.
let adj = (window.screen.width == window.outerWidth) ? 0 : -18
let maxScreenWidth = document.body.getBoundingClientRect().width * (window.screen.width / (window.outerWidth + adj))

Firefox:(!)

let maxScreenWidth = window.screen.width

如果您的解決方案適用於您提到的每種瀏覽器,則可以嘗試從用戶代理字符串中檢測到該瀏覽器,然后在該條件下應用正確的技術。 當然,用戶可以更改其用戶代理字符串,但這可能對您有用。

let isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
let isEdge = !isIE && !!window.StyleMedia;
let isFirefox = typeof InstallTrigger !== "undefined";
let isIE = /*@cc_on!@*/false || !!document.documentMode;
let isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));

if (isChrome) {
    let adj = (window.screen.width == window.outerWidth) ? 0 : -15;
    let maxScreenWidth = document.body.getBoundingClientRect().width * (window.screen.width / (window.outerWidth + adj));
} else if (isIE || isEdge) {
    let adj = (window.screen.width == window.outerWidth) ? 0 : -18
    let maxScreenWidth = document.body.getBoundingClientRect().width * (window.screen.width / (window.outerWidth + adj));
} else if (isFirefox) {
    let maxScreenWidth = window.screen.width;
} else {
    // Some default
}

或將其包裝到一個可以調用的函數中, maxScreenWidth從中返回maxScreenWidth

好的,這是我的解決方案。 等待錯誤報告響應,但與此同時:

var adj = (window.screen.width == window.outerWidth) ? 0 : -15
var maxScreenWidth = window.innerWidth * (window.screen.width / (window.outerWidth + adj))
if (parseInt(maxScreenWidth) != maxScreenWidth) maxScreenWidth = window.screen.width

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM