CSS3中的Media Queries学习笔记

少了你的风景,我没有美丽的人生。天气真好!一起去郊游吧,拥抱大自然,呼吸清新的空气,享受悠闲和温馨。

一、Media Queries 支持的属性

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

三、引用样式示例

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

四、内联样式示例

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


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

CSS Code复制内容到剪贴板
  1. @mediascreenand(max-width:900px){
  2. ...
  3. }

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

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

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

JavaScript Code复制内容到剪贴板
  1. /*AdjustthelayoutinIE8andlowerthanIE8whenthebrowserisnarrow*/
  2. functionprocessLowerIENavigate()
  3. {
  4. varisIE=document.all?1:0;
  5. if(isIE==1)
  6. {
  7. if(navigator.userAgent.indexOf("MSIE7.0")>0||navigator.userAgent.indexOf("MSIE8.0")>0)
  8. {
  9. //vardoc=document;
  10. varlink=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. varheads=document.getElementsByTagName("head");
  16. if(heads.length)
  17. heads[0].appendChild(link);
  18. else
  19. document.documentElement.appendChild(link);
  20. document.write("<scripttype='text/javascript'src='jquery.min.js'></script>");
  21. document.write("<scripttype='text/javascript'src='media_screen.js'></script>");
  22. }
  23. }
  24. }
  25. varlowerIE8=processLowerIENavigate();
  26. media_screen.js文件内容如下,直接从网上copy:
  27. functionadjustStyle(width){
  28. width=parseInt(width);
  29. if(width<902){
  30. //alert("<900");
  31. //alert(width);
  32. $("#size-stylesheet").attr("href","navigateLowerIE8.css");
  33. }else{
  34. //alert(">900");
  35. //alert(width);
  36. $("#size-stylesheet").attr("href","");
  37. }
  38. }
  39. $(function(){
  40. adjustStyle($(this).width());
  41. $(window).resize(function(){
  42. adjustStyle($(this).width());
  43. });
  44. });

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

以上就是CSS3中的Media Queries学习笔记。人的价值,在遭受诱惑的一瞬间被决定。更多关于CSS3中的Media Queries学习笔记请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
css让页脚保持在底部位置的四种方案

CSS如何使用Flex和Grid布局如何实现3D骰子

Flex布局史上最简单使用语法教程

新的CSS 伪类函数 :is() 和 :where()示例详解

纯CSS打字动画的如何实现示例