获取浏览器页面缩放或展示比例

翻页面的时候,不经意看到了 csdn还是什么页面 给提示‘您当前页面处于缩放,页面可能会错乱’;就好奇 怎么获取到 页面是否缩放和 缩放比例的,所以就查询了一下:得到了 下面的结果 做个记录;

原文地址:https://www.cnblogs.com/dyhao/p/11458882.html

 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);    
    }

控制台执行 代码后验证 还是很准确的;所以笔记 记录和分享一下;

发布了301 篇原创文章 · 获赞 197 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/boss_way/article/details/103033895