JavaScript ------ 獲取瀏覽器縮放比例,若不是100% ,給出提示


原由:彈窗問題,不再當前頁面內展示的數據,彈窗彈不出來,而且只要前面的彈出來,后面的也可彈出來了

多方測試以為與瀏覽器的滾動條有關

最后,偶然發現和瀏覽器的縮放有關系,當瀏覽器縮放比例為100%  的時候,未在當前頁面展示的數據,可以彈出,而不在此范圍的不行

而通過js 又禁止不了瀏覽器縮放,故通過js 進行設置,如果瀏覽器縮放比例不是 100% 給出提示。

   <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

   <script type="text/javascript">
   // 判斷pc瀏覽器是否縮放,若返回100則為默認無縮放,如果大於100則是放大,否則縮小
   function detectZoom (){
     var ratio = 0,
       screen = window.screen,
       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.onresize 事件可用於檢測頁面是否觸發了放大或縮小。
   $(function(){
     //alert(detectZoom())
   })
   $(window).on('resize',function(){
         isScale();
   });
   //判斷PC端瀏覽器縮放比例不是100%時的情況
   function isScale(){
     var rate = detectZoom();
     if(rate != 100){
       //如何讓頁面的縮放比例自動為100,'transform':'scale(1,1)'沒有用,又無法自動條用鍵盤事件,目前只能提示讓用戶如果想使用100%的比例手動去觸發按ctrl+0
       console.log(1)
      alert('當前頁面不是100%顯示,請按鍵盤ctrl+0恢復100%顯示標准,以防頁面顯示錯亂!')
       //var t = window.devicePixelRatio   // 獲取下載的縮放 125% -> 1.25    150% -> 1.5
     
     }
   }
    
   //阻止pc端瀏覽器縮放js代碼
   //由於瀏覽器菜單欄屬於系統軟件權限,沒發控制,我們着手解決ctrl/cammond + +/- 或 Windows下ctrl + 滾輪 縮放頁面的情況,只能通過js來控制了
    // jqeury version
   $(document).ready(function () {
     // chrome 瀏覽器直接加上下面這個樣式就行了,但是ff不識別
     $('body').css('zoom', 'reset');
     $(document).keydown(function (event) {
       //event.metaKey mac的command鍵
       if ((event.ctrlKey === true || event.metaKey === true)&& (event.which === 61 || event.which === 107 || event.which === 173 || event.which === 109 || event.which === 187  || event.which === 189)){
         event.preventDefault();
       }
     });
     $(window).bind('mousewheel DOMMouseScroll', function (event) {
       if (event.ctrlKey === true || event.metaKey) {
          event.preventDefault();
       }
     });
   
   });
   </script>

 防止頁面body 內容受到瀏覽器縮放影響,js  的 zoom 屬性

 <script>
     window.onload = function() {
        document.body.style.zoom = "normal"; //避免zoom尺寸疊加
        let scale = document.body.clientWidth / 1904;
        document.body.style.zoom = scale;
      };
      //防抖,避免resize占用過多資源
      (function() {
        var throttle = function(type, name, obj) {
          obj = obj || window;
          var running = false;
          var func = function() {
            if (running) {
              return;
            }
            running = true;
            requestAnimationFrame(function() {
              obj.dispatchEvent(new CustomEvent(name));
              running = false;
            });
          };
          obj.addEventListener(type, func);
        };

        throttle("resize", "optimizedResize");
      })();

      window.addEventListener("optimizedResize", function() {
        document.body.style.zoom = "normal";
        let scale = document.body.clientWidth / 1904;
        document.body.style.zoom = scale;
  });
    </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM