浏览器缩放检测

在pc端项目的开发中,经常会遇到用户浏览页面却发现布局出现混乱的情况,这时候排查可能是网页的缩放引起的,所以在页面加载的时候添加了浏览器的缩放检测提示功能。

采用的检测方式里用到一个属性 window.devicePixelRatio ,以下是摘自张鑫旭的 设备像素比devicePixelRatio简单介绍,有兴趣可以看下;

window.devicePixelRatio是设备上物理像素和设备独立像素(device-independent pixels (dips))的比例。
公式表示就是:window.devicePixelRatio = 物理像素 / dips

和screen.deviceXDPI 、 screen.logicalXDPI 都是为了计算缩放率

代码如下:

    // 浏览器缩放检测
    function detectZoom (){
        var ratio = 0,
            screen = window.screen,
            ua = navigator.userAgent.toLowerCase();
            
        if (window.devicePixelRatio !== undefined) {
            ratio = window.devicePixelRatio;
        }else if (~ua.indexOf('msie')) {  // ie
            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;
    };

之后可以使用这个比率来进行提示重置缩放,

150233_DJR2_3300735.png

另外经过检测的是chrome浏览器 win系统本身的缩放也会影响此缩放判断

 

转载于:https://my.oschina.net/mdu/blog/1821227

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值