CSS3中的Media Queries学习笔记
author:一佰互联 2019-04-21   click:167

一、Media Queries 支持的属性

二、关键字
and - 结合多个媒体查询 not - 排除某种制定的媒体类型 only - 用来定某种特定的媒体类型

三、引用样式示例

CSS Code复制内容到剪贴板
  1. <link rel="stylesheet" media="all" href="style.css" />   
  2. <link rel="stylesheet" media="screen and (min-width:980px)" href="desktop.css" />   
  3. <link rel="stylesheet" media="screen and (min-width:768px) and (max-width:980px)" href="pad.css" />   
  4. <link rel="stylesheet" media="screen and (min-width:480) and (max-width: 768px)" href="phone.css" />   
  5. <link rel="stylesheet" media="screen and (max-width:320px)" href="small.css" />  

四、内联样式示例

CSS Code复制内容到剪贴板
  1. @media screen and (min-width980px) {   
  2.     //css code  
  3. }   
  4. @screen and (min-width:768px) and (max-width:980px) {   
  5.     //css code  
  6. }   
  7. @screen and (min-width:480) and (max-width768px) {   
  8.     //css code  
  9. }   
  10. @screen and (max-width:320px) {   
  11.     //css code  
  12. }   
  13.   
  14. @media screen and (max-device-width480px) {   
  15.     //max-device-width等于480px  
  16. }   
  17. @media screen and (device-aspect-ratio: 16/9), screen and (device-aspect-ratio: 16/10) {   
  18.     //设备宽高比   
  19. }   
  20. @media all and (orientation:portrait) {   
  21.     //竖屏   
  22. }   
  23. @media all and (orientation:landscape) {   
  24.     //横屏   
  25. }  


五、I8的兼容性问题解决
问题根源:
在项目的CSS文件中采用了media这东东来根据视窗的大小自动调整布局,但是,但是IE8及以下版本浏览器不支持CSS3 media queries,也就是@media screen and (max-width: 900px) 里面的css代码没有执行,

CSS Code复制内容到剪贴板
  1. @media screen and (max-width900px) {   
  2.   ...   
  3. }  

这如何是好啊,网上倒是有不少人提出解决方法,最简单的方法就是:
IE8和之前的浏览器不支持CSS3 media queries,你可以在页面中添加css3-mediaqueries.js来解决这个问题。

XML/HTML Code复制内容到剪贴板
  1. <!--[if lt IE 9]>  
  2.     <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>  
  3. <![endif]-->  

原来如此,还挺简单嘛,结果大失所望啊,项目中怎么试怎么就不行呢,还望试过可行的同仁点拨点拨啊,没办法只能采用另一种稍微复杂些的方法,也是从网上学来,这里要考虑两个问题,一是只有IE8及其低版本才做此处理,二是只有浏览器缩小到某一个大小范围后才做此处理。方法如下:
原理:采用jquery,其实不懂,会用就行,然后在html中根据需要来改变对应的CSS文件
解决方法:
在所有页面的共用js文件后面添加如下代码:

JavaScript Code复制内容到剪贴板
  1. /*Adjust the layout in IE8 and lower than IE8 when the browser is narrow*/  
  2. function processLowerIENavigate()   
  3. {   
  4.    var isIE = document.all ? 1 : 0;   
  5.    if (isIE == 1)   
  6.    {   
  7.        if(navigator.userAgent.indexOf("MSIE7.0") > 0 || navigator.userAgent.indexOf("MSIE 8.0") > 0)   
  8.        {     
  9.     //  var doc=document;    
  10.            var link=document.createElement("link");   
  11.            link.setAttribute("rel""stylesheet");   
  12.            link.setAttribute("type""text/css");   
  13.            link.setAttribute("id""size-stylesheet");   
  14.            link.setAttribute("href""");   
  15.      
  16.            var heads = document.getElementsByTagName("head");   
  17.            if(heads.length)   
  18.                heads[0].appendChild(link);   
  19.            else  
  20.                document.documentElement.appendChild(link);   
  21.   
  22.            document.write("<script type="text/javascript" src="jquery.min.js"></script>");   
  23.            document.write("<script type="text/javascript" src="media_screen.js"></script>");   
  24.      
  25.        }   
  26.    }    
  27. }   
  28. var lowerIE8 = processLowerIENavigate();   
  29.   
  30. media_screen.js文件内容如下,直接从网上copy:   
  31. function adjustStyle(width) {   
  32.     width = parseInt(width);   
  33.     if (width < 902) {   
  34. //alert("<900");   
  35. //alert(width);   
  36.         $("#size-stylesheet").attr("href""navigateLowerIE8.css");   
  37.     } else {   
  38.       // alert(">900");   
  39.   //alert(width);   
  40.        $("#size-stylesheet").attr("href""");    
  41.     }   
  42. }   
  43.   
  44. $(function() {   
  45.     adjustStyle($(this).width());   
  46.     $(window).resize(function() {   
  47.         adjustStyle($(this).width());   
  48.     });   
  49. });  

navigateLowerIE8.css文件就是IE8其低版本浏览器在缩小时的页面布局了。OK,一切都确实搞定。