• javascript检测网页缩放演示代码


    一、为什么会提示浏览器显示比例不正常?


    在网上冲浪,有时在打某个网站时,会提示你的浏览器显示比例不是100%,建议你将浏览器显示比例恢复为100%,以便获得最佳显示效果。

    二、检测网页缩放比例的方法


    那么这些网站是如何检测你的浏览器显示比例的呢?

    (一)window.devicePixelRatio


    一般可以使用window.devicePixelRatio来检测。 window.devicePixelRatio当前显示设备的物理像素分辨率与 CSS 像素分辨率的比率,当其值为1时缩放率就是100%,如果其值小于1表示当前网页缩小显示了,如果其值天大于1表示当前网页放大显示了。所以利用该属性可以用于检测网页是否被缩放了。

    (二)window.outerWidth/window.innerWidth

    如果当前使用的浏览器不支持window.devicePixelRatio,那么我们可以使用window.outerWidth/window.innerWidth来测算,其中:window.outerWidth的值为浏览器窗口的外部宽度,包括所有界面元素(如工具栏/滚动条),而window.innerWidth的值为窗口内容区域的宽度,包括垂直滚动条(如果呈现的话)。

    (三)screen.deviceXDPI/screen.logicalXDPI

    对于IE浏览器来说,还可以使用screen.deviceXDPI/screen.logicalXDPI来测算。其中screen.deviceXDPI代表显示屏幕的每英寸实际水平点数,screen.logicalXDPI代表显示屏幕每英寸水平常规点数。

    综上,我们可以编写一个函数来返回当前网页显示比例:

    1. //功  能:取当前网页显示比例
    2. //返回值:当前网页显示比例,若未能获取有关数据,将返回0
    3. //更  新:20230914创建
    4. function getScrRatio()
    5. {
    6.     //(window.devicePixelRatio:当前显示设备的物理像素分辨率与 CSS 像素分辨率的比率
    7.     if(window.devicePixelRatio !== undefined)
    8.     {
    9.         return window.devicePixelRatio;    
    10.     }
    11.     //window.outerWidth:浏览器窗口的外部宽度,包括所有界面元素(如工具栏/滚动条)
    12.     //window.innerWidth:窗口内容区域的宽度,包括垂直滚动条(如果呈现的话)
    13.     if(window.outerWidth !== undefined && window.innerWidth !== undefined)
    14.     {
    15.         return window.outerWidth/window.innerWidth;
    16.     }
    17.     if(~navigator.userAgent.toLowerCase().indexOf('msie'))
    18.     {
    19.         //(IE提供)screen.deviceXDPI:显示屏幕的每英寸实际水平点数
    20.         //(IE提供)screen.logicalXDPI:显示屏幕每英寸水平常规点数
    21.         if(screen.deviceXDPI && screen.logicalXDPI)
    22.         {
    23.             return screen.deviceXDPI/screen.logicalXDPI;        
    24.         }
    25.     }
    26.     return 0;
    27. } //getScrRatio()


    三、演示代码


    我们在网页上放“显示数据”和“停止显示”两个按钮。其中“停止显示”按钮初始为禁用状态。

    当我们点击“显示数据”按钮,就定时采集和显示window.devicePixelRatio、window.outerWidth、window.innerWidth、screen.deviceXDPI/screen.logicalXDPI的值,并将“停止显示”按钮改为可用状态。

    当我们点击“停止显示”按钮,就停止更新数据,并将“停止显示”按钮恢复为禁用状态。

    1. html>
    2. <html>
    3.  <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="Generator" content="EditPlus®">
    6.   <meta name="Author" content="PurpleEndurer">
    7.   <meta name="Keywords" content="屏幕缩放比">
    8.   <meta name="Description" content="屏幕缩放">
    9.   <title>屏幕缩放比title>
    10.  head>
    11.  <body>
    12.  <button onclick="showDataTiming()">显示数据button>  <button id="btnStopShow" onclick="stopShow()">停止显示button>
    13.  <p>
    14.     浏览器类型:
    15. <script>
    16.     document.write(navigator.userAgent);
    17. script>
    18.  p>
    19.  <p>
    20.     devicePixelRatio:<span id="spanDevPR">span>
    21.  p>
    22.  <p>
    23.     window.outerWidth:<span id="spanWinOW">span>  
    24.     window.innerWidth:<span id="spanWinIW">span>
    25.  p>
    26.  <p>
    27.      window.screen.deviceXDPI:<span id="spanDevXDPI">span>  
    28.     window.screen.logicalXDPI:<span id="spanlogXDPI">span>
    29.  p>
    30.  <p>
    31.     屏幕缩放比:<span id="spanScrRadio">span>
    32.  <p>
    33. <script type="text/javascript">
    34. var btnStopShow = document.getElementById("btnStopShow");
    35. btnStopShow.disabled=true;
    36. var spanDevPR = document.getElementById("spanDevPR");
    37. var spanWinOW = document.getElementById("spanWinOW");
    38. var spanWinIW = document.getElementById("spanWinIW");
    39. var spanDevXDPI = document.getElementById("spanDevXDPI");
    40. var spanlogXDPI = document.getElementById("spanlogXDPI");
    41. var spanScrRadio =  document.getElementById("spanScrRadio");
    42. var t = 0;//定时器
    43. //功  能:取当前网页显示比例
    44. //返回值:当前网页显示比例,若未能获取有关数据,将返回0
    45. //更  新:20230914创建
    46. function getScrRatio()
    47. {
    48.     //(window.devicePixelRatio:当前显示设备的物理像素分辨率与 CSS 像素分辨率的比率
    49.     if(window.devicePixelRatio !== undefined)
    50.     {
    51.         return window.devicePixelRatio;    
    52.     }
    53.     //window.outerWidth:浏览器窗口的外部宽度,包括所有界面元素(如工具栏/滚动条)
    54.     //window.innerWidth:窗口内容区域的宽度,包括垂直滚动条(如果呈现的话)
    55.     if(window.outerWidth !== undefined && window.innerWidth !== undefined)
    56.     {
    57.         return window.outerWidth/window.innerWidth;
    58.     }
    59.     if(~navigator.userAgent.toLowerCase().indexOf('msie'))
    60.     {
    61.         //(IE提供)screen.deviceXDPI:显示屏幕的每英寸实际水平点数
    62.         //(IE提供)screen.logicalXDPI:显示屏幕每英寸水平常规点数
    63.         if(screen.deviceXDPI && screen.logicalXDPI)
    64.         {
    65.             return screen.deviceXDPI/screen.logicalXDPI;        
    66.         }
    67.     }
    68.     return 0;
    69. } //getScrRatio()
    70. function showData()
    71. {
    72.     if(undefined != window.devicePixelRatio)
    73.     {
    74.         spanDevPR.innerText = window.devicePixelRatio;    
    75.     }
    76.     if (undefined != window.outerWidth)
    77.     {
    78.         spanWinOW.innerText = window.outerWidth;
    79.     }
    80.     if (undefined != window.innerWidth)
    81.     {
    82.         spanWinIW.innerText = window.innerWidth;
    83.     }
    84.     if (undefined != screen.deviceXDPI)
    85.     {
    86.         spanDevXDPI.innerText = screen.deviceXDPI;
    87.     }
    88.     if (undefined != screen.logicalXDPI)
    89.     {
    90.         spanlogXDPI.innerText = screen.logicalXDPI;
    91.     }
    92.      var scrRatio = getScrRatio();
    93.     if (scrRatio)
    94.     {
    95.         spanScrRadio.innerText = Math.round(scrRatio*100); 
    96.     }
    97. }//showData()
    98. function showDataTiming()
    99. {
    100.     t = self.setInterval("showData()",500);
    101.     btnStopShow.disabled = false;
    102. }//showDataTiming()
    103. function stopShow()
    104. {
    105.     t = window.clearInterval(t);
    106.     btnStopShow.disabled = true;
    107. }//stopShow()
    108. script>
    109. body>
    110. html>

    四、代码运行效果


  • 相关阅读:
    heketi管理glusterfs,k8s基于glusterfs创建storageclass
    数据结构(C语言)——双链表
    C++的缺陷和思考(七)
    内存管理下及模板初阶
    python 按字段查询数据库
    PodMan容器技术
    自己编译的micropython esp32s3固件 pin.irq 无反应的天坑
    计算机视觉CV-目标检测OB
    软件工程-从规划、需求(DFD数据流图),到设计、实现和测试
    STM32系列(HAL库)——串口IAP
  • 原文地址:https://blog.csdn.net/Purpleendurer/article/details/132912795