JavaScript方法獲取屏幕縮放比例與分辨率
發(fā)布日期:2022/9/12 7:25:15 瀏覽量:
開發(fā)一些前端頁面的時(shí)候,會(huì)考慮到一些設(shè)備屏幕的兼容性問題,這個(gè)時(shí)候需要根據(jù)屏幕的分辨率以及縮放比例來進(jìn)行實(shí)時(shí)的寬高設(shè)定,所以我總結(jié)了用JavaScript方法進(jìn)行獲取用戶電腦屏幕的分辨率和縮放比例的數(shù)據(jù):

獲取屏幕縮放比例
//獲取屏幕縮放比例
function getRatio()
{
var ratio=0;
var screen=window.screen;
var ua=navigator.userAgent.toLowerCase();
if(window.devicePixelRatio !== undefined)
{
ratio=window.devicePixelRatio;
}
else if(~ua.indexOf(’msie’))
{
if(screen.deviceXDPI && screen.logicalXDPI)
{
ratio=screen.deviceXDPI/screen.logicalXDPI;
}
}
else if(window.outerWidth !== undefined && window.innerWidth !== undefined)
{
ratio=window.outerWidth/window.innerWidth;
}
if(ratio)
{
ratio=Math.round(ratio*100);
}
return ratio;
}
獲取分辨率
屏幕分辨率的高:window.screen.height*getRatio()/100 //乘以縮放比例為真實(shí)的像素
屏幕分辨率的寬:window.screen.width*getRatio()/100 //乘以縮放比例為真實(shí)的像素
屏幕可用工作區(qū)高度:window.screen.availHeight;
屏幕可用工作區(qū)寬度:window.screen.availWidth;
馬上咨詢: 如果您有業(yè)務(wù)方面的問題或者需求,歡迎您咨詢!我們帶來的不僅僅是技術(shù),還有行業(yè)經(jīng)驗(yàn)積累。
QQ: 39764417/308460098 Phone: 13 9800 1 9844 / 135 6887 9550 聯(lián)系人:石先生/雷先生