css兼容處理-hack

  1.  瀏覽器兼容之旅的第二站:各瀏覽器的Hack寫法
  2.  Browser CSS Hacks
  3.  Moving IE specific CSS into @media blocks
  4.  Detecting browsers javascript hacks
  5.  Browser Specific Hacks
  6.  Browser-Specific CSS Hacks
  7.  CSS hacks

你們平時看得多的應該是IE瀏覽器的hack寫法比較多,但對於現代瀏覽器,好比說Safari、Chrome、Firefox等瀏覽器的hack寫法並很少見,甚至有的不知道怎麼寫。若是您是屬於後者,根本不知道各瀏覽器下具備哪些hack手段,不要着急。由於Hugo GiraudelTim Pietrusky將各瀏覽器下的hack寫整理放在了Browserhacks.com之上。javascript

1、Chrome瀏覽器

選擇器Hackphp

/* Chrome 24- and Safari 5- */
::made-up-pseudo-element, .selector { 代碼放在這裏 } 

媒體查詢Hackscss

/* Chrome, Safari 3+ */
@media screen and (-webkit-min-device-pixel-ratio:0) { 代碼放在這裏 } 

JavaScript Hackhtml

/* Chrome */
var isChrome = Boolean(window.chrome); 

2、Firefox瀏覽器

屬性選擇器Hackjava

/* Firefox 1.5 */
body:empty .selector { 樣式代碼放這裏 } /* Firefox 2+ */ .selector, x:-moz-any-link { 樣式代碼放這裏 } /* Firefox 3+ */ .selector, x:-moz-any-link; x:default { 樣式代碼放這裏 } /* Firefox 3.5+ */ body:not(:-moz-handler-blocked) .selector { 樣式代碼放這裏 } 

媒體查詢Hackweb

/* Firefox 3.5+, IE 9/10, Opera */
@media screen and (min-resolution: +72dpi) { 樣式代碼放這裏 } /* Firefox 3.6+ */ @media screen and (-moz-images-in-menus:0) { 樣式代碼放這裏 } /* Firefox 4+ */ @media screen and (min--moz-device-pixel-ratio:0) { 樣式代碼放這裏 } 

JavaScript Hackchrome

/* Firefox */
var isFF = !!navigator.userAgent.match(/firefox/i); /* Firefox 2 - 13 */ var isFF = Boolean(window.globalStorage); /* Firefox 2/3 */ var isFF = /a/[-1]=='a'; /* Firefox 3 */ var isFF = (function x(){})[-5]=='x'; 

混合型Hack瀏覽器

/* Firefox 3+ */
@-moz-document url-prefix() { 樣式代碼放這裏 } 

3、Opera瀏覽器

屬性選擇器Hackapp

/* Opera 9.25, Safari 2/3.1 */ *|html[xmlns*=""] .selector { 樣式代碼放這裏 } /* Opera 9.27 and below, Safari 2 */ html:first-child .selector { 樣式代碼放這裏 } /* Opera 9.5+ */ noindex:-o-prefocus, .selector { 樣式代碼放這裏 } 

媒體查詢Hackurl

/* Opera 7 */ @media all and (min-width: 0px){ 樣式代碼放這裏 } /* Opera 12- */ @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { 樣式代碼放這裏 } /* Opera, Firefox 3.5+, IE 9/10 */ @media screen and (min-resolution: +72dpi) { 樣式代碼放這裏 } /* Opera, IE 8/9/10 */ @media screen { 樣式代碼放這裏 } 

JavaScript Hack

/* Opera 9.64- */
var isOpera = /^function \(/.test([].sort); /* Opera 12- */ var isOpera = Boolean(window.opera); 

4、Safari瀏覽器

屬性選擇器Hack

/* Safari 2/3 */ html[xmlns*=""] body:last-child .selector { 樣式代碼放這裏 } html[xmlns*=""]:root .selector { 樣式代碼放這裏 } /* Safari 2/3.1, Opera 9.25 */ *|html[xmlns*=""] .selector { 樣式代碼放這裏 } /* Safari 5- and Chrome 24- */ ::made-up-pseudo-element, .selector { 樣式代碼放這裏 } 

媒體查詢Hack

/* Safari */
var isSafari = /a/.__proto__=='//'; 

5、IE瀏覽器

選擇器Hack

/* IE 6 and below */
* html .selector { 樣式代碼放這裏 } .suckyie6.selector { 樣式代碼放這裏 } /* .suckyie6 can be any unused class */ /* IE 7 and below */ .selector, { 樣式代碼放這裏 } /* IE 7 */ *:first-child+html .selector { 樣式代碼放這裏 } .selector, x:-IE7 { 樣式代碼放這裏 } *+html .selector { 樣式代碼放這裏 } /* Everything but IE 6 */ html > body .selector { 樣式代碼放這裏 } /* Everything but IE 6/7 */ html > /**/ body .selector { 樣式代碼放這裏 } head ~ /* */ body .selector { 樣式代碼放這裏 } /* Everything but IE 6/7/8 */ :root *> .selector { 樣式代碼放這裏 } body:last-child .selector { 樣式代碼放這裏 } body:nth-of-type(1) .selector { 樣式代碼放這裏 } body:first-of-type .selector { 樣式代碼放這裏 } 

屬性/屬性值 Hack

/* IE 6 */
.selector { _color: blue; } 
.selector { -color: blue; }

/* IE 6/7 - any combination of these characters: ! $ & * ( ) = % + @ , . / ` [ ] # ~ ? : < > | */ .selector { !color: blue; } .selector { $color: blue; } .selector { &color: blue; } .selector { *color: blue; } /* ... */ /* IE 6/7 - acts as an !important */ .selector { color: blue !ie; } /* string after ! can be anything */ /* IE 8/9 */ .selector { color: blue\0/; } /* must go at the END of all rules */ /* IE 9/10 */ .selector:nth-of-type(1n) { color: blue\9; } /* IE 6/7/8/9/10 */ .selector { color: blue\9; } .selector { color/*\**/: blue\9; } /* Everything but IE 6 */ .selector { color/**/: blue; } 

媒體查詢Hack

/* IE 6/7 */
@media screen\9 { 樣式代碼放這裏 } /* IE 6/7/8 */ @media \0screen\,screen\9 { 樣式代碼放這裏 } /* IE 8 */ @media \0screen { 樣式代碼放這裏 } /* IE 8/9/10 & Opera */ @media screen\0 { 樣式代碼放這裏 } /* IE 9/10, Firefox 3.5+, Opera */ @media screen and (min-resolution: +72dpi) { 樣式代碼放這裏 } /* IE 9/10 */ @media screen and (min-width:0\0) { 樣式代碼放這裏 } /* IE 10+ */ @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { 樣式代碼放這裏 } /* Everything but IE 6/7/8 */ @media screen and (min-width: 400px) { 樣式代碼放這裏 } 

JavaScript Hack

/* IE <= 8 */ var isIE = '\v'=='v'; 
/* IE 6 */
(checkIE = document.createElement("b")).innerHTML = "<!--[if IE 6]><i></i><![endif]-->"; 
var isIE = checkIE.getElementsByTagName("i").length == 1;	
/* IE 7 */
(checkIE = document.createElement("b")).innerHTML = "<!--[if IE 7]><i></i><![endif]-->"; 
var isIE = checkIE.getElementsByTagName("i").length == 1;
navigator.appVersion.indexOf("MSIE 7.")!=-1	
/* IE 8 */
(checkIE = document.createElement("b")).innerHTML = "<!--[if IE 8]><i></i><![endif]-->"; 
var isIE = checkIE.getElementsByTagName("i").length == 1;	
/* IE 9 */
(checkIE = document.createElement("b")).innerHTML = "<!--[if IE 9]><i></i><![endif]-->"; 
var isIE = checkIE.getElementsByTagName("i").length == 1;	
/* IE 10 */
var isIE = eval("/*@cc_on!@*/false") && document.documentMode === 10; /* IE 10 */ var isIE = document.body.style.msTouchAction != undefined; 

上面列出各個瀏覽器下hack的寫法,固然在實際運用之中並不建議使用hack。若是你的頁面須要hack來處理時,請先檢查你的css或者js,若是實在沒法達到要求,在考慮使用hack來處理。

 

轉載自http://www.w3cplus.com/

相關文章
相關標籤/搜索