js獲取屏幕高度寬度分辨率問題,當css3的@media查詢寫法不能解決問題時請看這個


當css的@media查詢寫法如下:

@media screen and (max-width: 1920px) {
    .feature-table {

      height: 980px;background-color: #AAAAAA;    

    }

}
@media screen and (max-width: 1809px) {
    .feature-table {
        height: 850px;background-color: #AAAAAA;
    }
}
@media screen and (max-width: 1559px) {
    .feature-table {
        height: 700px;background-color: #AAAAAA;
    }
}
@media screen and (max-width: 1360px) {
    .feature-table {
        height: 500px;background-color: #AAAAAA;
    }
}
@media screen and (max-width: 1309px) {
    .feature-table {
        height: 700px;background-color: #AAAAAA;
    }
}
@media screen and (max-width: 1059px) {
    .feature-table {
        height: 400px;background-color: #AAAAAA;
    }
}
@media screen and (max-width: 809px) {
    .feature-table {
        height: 300px;background-color: #AAAAAA;
    }
}

它根據屏幕分辨率的寬度去設置id爲feature-table的元素的高度和背景色,但是存在如下情況1920*1200與1920*1080兩種分辨率的寬度一樣,高度不一樣。從上面的代碼走向來說,id爲feature-table的元素的高度都會被設成980px,有可能在1920*1200分辨率下,該元素正好鋪滿,而1920*1080下溢出影響顯示效果。

但是css3中好像沒有@media screen and (max-height: 809px) {}的寫法,因此可以藉助js獲取屏幕的高度,根據屏幕的高度去顯示內容。代碼如下

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div  id="size">
asdasda
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
// 測試引用是否成功

$(document).ready(function(e) {

      var s = "";   

      s += " 網頁可見區域寬:"+ document.body.clientWidth+"\n";    
      s += " 網頁可見區域高:"+ document.body.clientHeight+"\n";    
      s += " 網頁可見區域寬:"+ document.body.offsetWidth + " (包括邊線和滾動條的寬)"+"\n";    
      s += " 網頁可見區域高:"+ document.body.offsetHeight + " (包括邊線的寬)"+"\n";    
      s += " 網頁正文全文寬:"+ document.body.scrollWidth+"\n";    
      s += " 網頁正文全文高:"+ document.body.scrollHeight+"\n";    
      s += " 網頁被捲去的高(ff):"+ document.body.scrollTop+"\n";    
      s += " 網頁被捲去的高(ie):"+ document.documentElement.scrollTop+"\n";    
      s += " 網頁被捲去的左:"+ document.body.scrollLeft+"\n";    
      s += " 網頁正文部分上:"+ window.screenTop+"\n";    
      s += " 網頁正文部分左:"+ window.screenLeft+"\n";    
      s += " 屏幕分辨率的高:"+ window.screen.height+"\n";    
      s += " 屏幕分辨率的寬:"+ window.screen.width+"\n";    
      s += " 屏幕可用工作區高度:"+ window.screen.availHeight+"\n";    
      s += " 屏幕可用工作區寬度:"+ window.screen.availWidth+"\n";    
      s += " 你的屏幕設置是 "+ window.screen.colorDepth +" 位彩色"+"\n";    
      s += " 你的屏幕設置 "+ window.screen.deviceXDPI +" 像素/英寸"+"\n";    
      alert(s);
  //當屏幕高度爲1200px時,增加背景色
  if(window.screen.height==1200){
$("#size").css("background-color","#121111");
}
  });
</script>
</body>

</html>

這中貌似很難用css寫,最好就是js來獲取高度後,動態修改其他元素、

發佈了28 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章