學(xué)習(xí)一下IE10和IE11的CSS Hack

2013-07-05 09:32:03來(lái)源:WEB前端開發(fā)作者:

有IE就有hack,看看IE9的css hack,IE8的css hack;上次同事說(shuō)一個(gè)頁(yè)面IE10下有問(wèn)題,IE9下測(cè)試了一下,也有同樣的問(wèn)題。悲劇了趕緊找IE10的hack。

有IE就有hack,看看IE9的css hack,IE8的css hack;上次同事說(shuō)一個(gè)頁(yè)面IE10下有問(wèn)題,IE9下測(cè)試了一下,也有同樣的問(wèn)題。悲劇了趕緊找IE10的hack。

google上翻到的IE10 CSS Hacks 還算比較實(shí)用的。記錄一下以備后用。

一、特性檢測(cè):@cc_on

我們可以用IE私有的條件編譯(conditional compilation)結(jié)合條件注釋來(lái)提供針對(duì)ie10的Hack:該腳本里面的IE排除條件注釋,以確保IE6-9不承認(rèn)它,然后它功能檢測(cè)到了名為@ cc_on。

1
2
3
4
5
<!--[if !IE]><!--><script>
if (/*@cc_on!@*/false) {
    document.documentElement.className+=' ie10';
}
</script><!--<![endif]-->

請(qǐng)注意/*@cc_on ! @*/中間的這個(gè)感嘆號(hào)。

這樣就可以在ie10中給html元素添加一個(gè)class=”ie10″,然后針對(duì)ie10的樣式可以卸載這個(gè)這個(gè)選擇器下:

1
2
3
.ie10 .example {
   /* IE10-only styles go here */
}

這是ie10標(biāo)準(zhǔn)模式下的截圖:

Snip20130702_1

這是ie10,IE8模式下的截圖:

Snip20130702_2

考錄到兼容以后的IE版本,比如IE11,js代碼可以改一下:

1
2
3
if (/*@cc_on!@*/false) {
    document.documentElement.className+=' ie' + document.documentMode;
}

關(guān)于document.documentMode可以查看IE的documentMode屬性(IE8+新增)。

可能是想多了,實(shí)事上經(jīng)測(cè)試預(yù)覽版的IE11已經(jīng)不支持@ cc_on語(yǔ)句,不知道正式版會(huì)不會(huì)支持。不過(guò)這樣區(qū)分IE11倒是一件好事。這樣修改代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>無(wú)標(biāo)題文檔</title>
    <!--[if !IE]><!-->
    <script>
        // 針對(duì)IE10
        if (/*@cc_on!@*/false) {
            document.documentElement.className += ' ie' + document.documentMode;
        }
        // 針對(duì)IE11及非IE瀏覽器,
        // 因?yàn)镮E11下document.documentMode為11,所以html標(biāo)簽上會(huì)加ie11樣式類;
        // 而非IE瀏覽器的document.documentMode為undefined,所以html標(biāo)簽上會(huì)加ieundefined樣式類。
        if (/*@cc_on!@*/true) {
            document.documentElement.className += ' ie' + document.documentMode;
        }
    </script>
    <!--<![endif]-->
    <style type="text/css">
        .ie10 .testclass {
            color: red
        }
        .ie11 .testclass {
            color: blue
        }
        .ieundefined  .testclass {
            color: green
        }
    </style>
</head>
 
<body>
<div class="testclass">
    test text!
</div>
</body>
</html>

其中:

1
2
3
if (/*@cc_on!@*/true) {
    document.documentElement.className += ' ie' + document.documentMode;
}

以上代碼是針對(duì)IE11及非IE瀏覽器,因?yàn)?

  • IE11下document.documentMode為11,所以html標(biāo)簽上會(huì)加ie11樣式類;
  • 而非IE瀏覽器的document.documentMode為undefined,所以html標(biāo)簽上會(huì)加ieundefined樣式類。

這樣把IE11也區(qū)分出來(lái)了,IE11預(yù)覽版下的截圖:

Snip20130702_4

呵呵,純屬YY,IE11正式版還不知道什么樣子,而且在實(shí)際的項(xiàng)目中隨著IE的逐漸標(biāo)準(zhǔn)化,IE11和IE10可能很少用不到css hack。

二、@media -ms-high-contrast 方法

IE10支持媒體查詢,然后也支持-ms-high-contrast這個(gè)屬性,所以,我們可以用它來(lái)Hack IE10:

1
2
3
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
   /* IE10-specific styles go here */
}

這種寫法可以適配到高對(duì)比度和默認(rèn)模式。所以可以覆蓋到所有ie10的模式了。這種方式在預(yù)覽版的IE11中也生效。

當(dāng)然,方法二也可以和方法一一起用:

1
2
3
if (window.matchMedia("screen and (-ms-high-contrast: active), (-ms-high-contrast: none)").matches) {
    document.documentElement.className += "ie10";
}

三、@media 0 方法

這個(gè)方法不是太完美,因?yàn)镮E9和預(yù)覽版的IE11也支持media和\0的hack。

1
2
3
@media screen and (min-width:0\0) {
    /* IE9 , IE10 ,IE11 rule sets go here */
}

總之,隨著IE的逐漸標(biāo)準(zhǔn)化,IE11和IE10可能很少用不到css hack,不看也罷,呵呵。
關(guān)鍵詞:IE10CSS

贊助商鏈接: