CSS3中的Media Queries學(xué)習(xí)筆記

一、Media Queries 支持的屬性
二、關(guān)鍵字
and - 結(jié)合多個(gè)媒體查詢 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" />
四、內(nèi)聯(lián)樣式示例
- @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) {
- //設(shè)備寬高比
- }
- @media all and (orientation:portrait) {
- //豎屏
- }
- @media all and (orientation:landscape) {
- //橫屏
- }
五、I8的兼容性問題解決
問題根源:
在項(xiàng)目的CSS文件中采用了media這東東來根據(jù)視窗的大小自動(dòng)調(diào)整布局,但是,但是IE8及以下版本瀏覽器不支持CSS3 media queries,也就是@media screen and (max-width: 900px) 里面的css代碼沒有執(zhí)行,
- @media screen and (max-width: 900px) {
- ...
- }
這如何是好啊,網(wǎng)上倒是有不少人提出解決方法,最簡單的方法就是:
IE8和之前的瀏覽器不支持CSS3 media queries,你可以在頁面中添加css3-mediaqueries.js來解決這個(gè)問題。
- <!--[if lt IE 9]>
- <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
- <![endif]-->
原來如此,還挺簡單嘛,結(jié)果大失所望啊,項(xiàng)目中怎么試怎么就不行呢,還望試過可行的同仁點(diǎn)撥點(diǎn)撥啊,沒辦法只能采用另一種稍微復(fù)雜些的方法,也是從網(wǎng)上學(xué)來,這里要考慮兩個(gè)問題,一是只有IE8及其低版本才做此處理,二是只有瀏覽器縮小到某一個(gè)大小范圍后才做此處理。方法如下:
原理:采用jquery,其實(shí)不懂,會(huì)用就行,然后在html中根據(jù)需要來改變對(duì)應(yīng)的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文件內(nèi)容如下,直接從網(wǎng)上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其低版本瀏覽器在縮小時(shí)的頁面布局了。OK,一切都確實(shí)搞定。
相關(guān)文章
- 這篇文章主要給大家介紹了關(guān)于CSS3 @media的基本用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用CSS3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)2019-09-10
詳解使用CSS3的@media來編寫響應(yīng)式的頁面
這篇文章主要介紹了詳解使用CSS3的@media來編寫響應(yīng)式的頁面,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-01CSS3 media queries + jQuery實(shí)現(xiàn)響應(yīng)式導(dǎo)航
這篇文章主要介紹了CSS3 media queries + jQuery實(shí)現(xiàn)響應(yīng)式導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-30css3 media 響應(yīng)式布局的簡單實(shí)例
下面小編就為大家?guī)硪黄猚ss3 media 響應(yīng)式布局的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-03- 下面小編就為大家?guī)硪黄猚ss3media響應(yīng)式布局實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-08
- 這篇文章主要介紹了CSS3 Media Queries中媒體屬性的使用,文章中還以一個(gè)響應(yīng)式設(shè)計(jì)的例子作為補(bǔ)充講解,需要的朋友可以參考下2016-02-29
使用 CSS3 中@media 實(shí)現(xiàn)網(wǎng)頁自適應(yīng)的示例代碼
這篇文章主要介紹了使用 CSS3 中@media 實(shí)現(xiàn)網(wǎng)頁自適應(yīng)的示例代碼,代碼簡單易懂,非常不錯(cuò)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-24