javaSript 處理電腦和瀏覽器pc端縮放對頁面的影響


由於用戶的個人習慣或其他原因,可能會調整到電腦上的縮放比例或者瀏覽器的縮放比例,如果訪問頁面會看起來比較不好看,

電腦上的縮放:

 

瀏覽器上的縮放:

 

 

解決方法

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.onload = window.onresize = function () {
  if(detectZoom() > 100){ //當前電腦/瀏覽器縮放大於100%, 按等比縮小; 當電腦和瀏覽器都存在縮放,值會累計加在一起的;
    document.body.style.zoom = (100/detectZoom()).toFixed(2);
  }else{
    document.body.style.zoom = 1;
  }
};

 


免責聲明!

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



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