首頁  >  文章  >  web前端  >  分享CSS3中的Media Queries的學習總結

分享CSS3中的Media Queries的學習總結

高洛峰
高洛峰原創
2017-03-10 09:41:491741瀏覽

CSS3中的Media Queries經常被用來製作前端的響應式設計頁面,這裡分享CSS3中的Media Queries的學習總結,包括IE8中的兼容問題解決,需要的朋友可以參考下

#一、Media Queries 支援的屬性
分享CSS3中的Media Queries的学习总结

分享CSS3中的Media Queries的学习总结


##and -結合多個媒體查詢not - 排除某種制定的媒體類型only - 用來定某種特定的媒體類型

#三、引用樣式範例
##

<link rel="stylesheet" media="all" href="style.css" />   
<link rel="stylesheet" media="screen and (min-width:980px)" href="desktop.css" />   
<link rel="stylesheet" media="screen and (min-width:768px) and (max-width:980px)" href="pad.css" />   
<link rel="stylesheet" media="screen and (min-width:480) and (max-width: 768px)" href="phone.css" />   
<link rel="stylesheet" media="screen and (max-width:320px)" href="small.css" />


四、內聯樣式範例

@media screen and (min-width: 980px) {   
    //css code
}   
@screen and (min-width:768px) and (max-width:980px) {   
    //css code
}   
@screen and (min-width:480) and (max-width: 768px) {   
    //css code
}   
@screen and (max-width:320px) {   
    //css code
}   

@media screen and (max-device-width: 480px) {   
    //max-device-width等于480px
}   
@media screen and (device-aspect-ratio: 16/9), screen and (device-aspect-ratio: 16/10) {   
    //设备宽高比   
}   
@media all and (orientation:portrait) {   
    //竖屏   
}   
@media all and (orientation:landscape) {   
    //横屏   
}




#

@media screen and (max-width: 900px) {   
  ...   
}




五、 I8的兼容性問題解決

問題根源:

在專案的CSS檔案中採用了media這東東來根據視窗的大小自動調整佈局,但是,但是IE8及以下版本瀏覽器不支援CSS3 media queries,也就是@media screen and (max-width: 900px) 裡面的css程式碼沒有執行,

<!--[if lt IE 9]>
    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
<![endif]-->




#這如何是好啊,網路上倒是有不少人提出解決方法,最簡單的方法就是:
IE8和之前的瀏覽器不支援CSS3 media queries,你可以在頁面中加入css3-mediaqueries.js來解決這個問題。

/*Adjust the layout in IE8 and lower than IE8 when the browser is narrow*/
function processLowerIENavigate()   
{   
   var isIE = document.all ? 1 : 0;   
   if (isIE == 1)   
   {   
       if(navigator.userAgent.indexOf("MSIE7.0") > 0 || navigator.userAgent.indexOf("MSIE 8.0") > 0)   
       {     
    //  var doc=document; 
           var link=document.createElement("link");   
           link.setAttribute("rel", "stylesheet");   
           link.setAttribute("type", "text/css");   
           link.setAttribute("id", "size-stylesheet");   
           link.setAttribute("href", "");   

           var heads = document.getElementsByTagName("head");   
           if(heads.length)   
               heads[0].appendChild(link);   
           else
               document.documentElement.appendChild(link);   

           document.write("<script type=&#39;text/javascript&#39; src=&#39;jquery.min.js&#39;></script>");   
           document.write("<script type=&#39;text/javascript&#39; src=&#39;media_screen.js&#39;></script>");   

       }   
   }    
}   
var lowerIE8 = processLowerIENavigate();   

media_screen.js文件内容如下,直接从网上copy:   
function adjustStyle(width) {   
    width = parseInt(width);   
    if (width < 902) {   
//alert("<900");
//alert(width);
        $("#size-stylesheet").attr("href", "navigateLowerIE8.css");   
    } else {   
      // alert(">900");
  //alert(width);
       $("#size-stylesheet").attr("href", "");    
    }   
}   

$(function() {   
    adjustStyle($(this).width());   
    $(window).resize(function() {   
        adjustStyle($(this).width());   
    });   
});

原來如此,還挺簡單嘛,結果大失所望啊,專案中怎麼試怎麼就不行呢,還望試過可行的同仁點撥點撥啊,沒辦法只能採用另一種稍微複雜些的方法,也是從網上來,這裡要考慮兩個問題,一是只有IE8及其低版本才做此處理,二是只有瀏覽器縮小到某一個大小範圍後才做此處理。方法如下:###原理:採用jquery,其實不懂,會用就行,然後在html中根據需要來改變對應的CSS檔案###解決方法:###在所有頁面的共用js檔案後面加上如下程式碼:######rrreee############navigateLowerIE8.css檔案就是IE8其低版瀏覽器在縮小時的頁面佈局了。 OK,一切都確實搞定​​。 ###

以上是分享CSS3中的Media Queries的學習總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn