您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/3/21 5:22:42发布32次查看
css3中的media queries经常被用来制作前端的响应式设计页面,这里分享css3中的media queries的学习总结,包括ie8中的兼容问题解决,需要的朋友可以参考下
一、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) { //横屏 }
五、i8的兼容性问题解决
问题根源:
在项目的css文件中采用了media这东东来根据视窗的大小自动调整布局,但是,但是ie8及以下版本浏览器不支持css3 media queries,也就是@media screen and (max-width: 900px) 里面的css代码没有执行,
@media screen and (max-width: 900px) { ... }
这如何是好啊,网上倒是有不少人提出解决方法,最简单的方法就是:
ie8和之前的浏览器不支持css3 media queries,你可以在页面中添加css3-mediaqueries.js来解决这个问题。
<!--[if lt ie 9]> <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script> <![endif]-->
原来如此,还挺简单嘛,结果大失所望啊,项目中怎么试怎么就不行呢,还望试过可行的同仁点拨点拨啊,没办法只能采用另一种稍微复杂些的方法,也是从网上学来,这里要考虑两个问题,一是只有ie8及其低版本才做此处理,二是只有浏览器缩小到某一个大小范围后才做此处理。方法如下:
原理:采用jquery,其实不懂,会用就行,然后在html中根据需要来改变对应的css文件
解决方法:
在所有页面的共用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='text/javascript' src='jquery.min.js'></script>"); document.write("<script type='text/javascript' src='media_screen.js'></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()); }); });
navigatelowerie8.css文件就是ie8其低版本浏览器在缩小时的页面布局了。ok,一切都确实搞定。
以上就是分享css3中的media queries的学习总结的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product