解決ie6在css上的兼容問題

ie6上css碰到的坑

前兩天在給一個項目做東西的時候,碰到一個有意思的項目,是需要兼容ie6,有一些碰到並且解決的問題,給大家寫下來,方便大家以後碰到類似的問題哈~

喜歡的話還請點贊!

1.important支持的不夠好

1.1爲什麼說不夠好?

important某些情況下不能決定最終的樣式屬性。

1.2代碼!代碼!!

我們通過對顏色的控制說明這一切~

<style type="text/css">
    div {
        width: 100px;
        height: 100px;
        border: aliceblue 2px solid;
    }

    .frist {
        background-color: red !important;
        background-color: blue;
    }

    .second {
        background-color: red !important;
    }

    .third {
        background-color: blue;
    }

    .second {
        background-color: blue;
    }
</style>
<div class="frist"></div>
<div class="second third"></div>
<div class="second"></div>

1.3 上圖!上圖!!

谷歌 ie6

1.4我們發現了啥?

1.在同一個css代碼塊中的important沒那麼強力,會被後續的樣式覆蓋;
2.不過如果不再同一個css代碼塊中寫的話,你大爺終究是你大爺~
3.所以我們可以利用這個漏洞,寫ie6專有的樣式了(僞hack)(慎用,不如用ie6的hack寫法“_”)

2.margin雙倍問題

2.1出現原因

當float和margin同時設置時,就會出現margin雙倍的問題

2.2代碼代碼!

<style type="text/css">
     /** 包裹區 **/
    .area {
        width: 200px;
        height: 200px;
        background-color: #00FFFF;
    }

     /** 底部指示區 **/
    .footer {
        width: 200px;
        height: 100px;
        background-color: royalblue;
    }

     /** 測試,margin 的代碼塊 **/
    .testMargin {
        width: 200px;
        height: 100px;
        float: left;
        margin: 50px;
        background-color: #0079CC;
    }

     /** 50px 指示區 **/
    .checkLine {
        width: 50px;
        float: left;
        height: 100px;
        display: inline-block;
        background-color: #000;
    }

     /** 100px 指示區 **/
    .checkLine2 {
        width: 50px;
        height: 100px;
        display: inline-block;
        background-color: teal;
    }
</style>
<div class="area">
    <div class="testMargin"></div>
</div>
<div class="footer">
    <!-- 50px 指示塊 -->
    <span class="checkLine"></span>
    <!-- 100px 指示塊 -->
    <span class="checkLine2"></span>
</div>

2.3來看看效果

谷歌 ie6

2.4.怎麼解決?

方案1:

將div設置display:inline

.testMargin {
    width: 200px;
    height: 100px;
    float: left;
    display: inline;
    margin: 50px;
    background-color: #0079CC;
}

方案2:

編寫ie6的hack

.testMargin {
    width:200px;
    height:100px;
    float:left;
    margin:50px;
    background-color:#0079CC;
    _margin: 50px 25px;
}

2.5解決結果

3.ie6下圖片的會帶有藍灰色背景色

3.1 css代碼

<style type="text/css">
    .pngImg {
        border: #FF0000 1px solid;
        width: 200px;
        height: 200px;
    }

    .jpgImg {
        border: black 1px solid;
        width: 200px;
        height: 200px;
    }

    .pngSpan {
        border: blue 1px solid;
        display: inline-block;
        width: 200px;
        height: 200px;
        background: transparent url(https://jhcan333.github.io/can-Share/image/ie6/404.png) no-repeat center top;
        background-size: cover;
        _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/ie6/404.png", sizingMethod='scale'); /*IE6有效*/
        _background: none; /*IE6有效*/
    }

    .jpgSpan {
        border: brown 1px solid;
        display: inline-block;
        width: 200px;
        height: 200px;
        background: transparent url(https://jhcan333.github.io/can-Share/image/ie6/404.jpg) no-repeat center top;
        background-size: contain;
        _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/ie6/404.jpg", sizingMethod='scale'); /*IE6有效*/
        _background: none; /*IE6有效*/
    }
</style>

3.2 html標籤

<span class="pngSpan"></span>
<img class="pngImg" src="https://jhcan333.github.io/can-Share/image/ie6/404.png">
<span class="jpgSpan"></span>
<img class="jpgImg" src="https://jhcan333.github.io/can-Share/image/ie6/404.jpg">

3.3展示效果

1.谷歌瀏覽器

2.IE6瀏覽器

3.4怎麼搞

IE6不支持png背景透明或半透明,所以img標籤中的圖片會帶有背景色,需要藉助css濾鏡來實現

_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="https://jhcan333.github.io/can-Share/image/404.png",sizingMethod='scale');/*IE6有效*/
_background:none;/*IE6有效*/

在這裏,首先把background取消,然後使用css濾鏡來設置。其中屬性前面添加”_”下劃線,來表示,只在ie6上使用。

3.5現有的封裝好的方法

<script>
    function correctPNG() {
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])
        if((version >= 5.5) && (document.body.filters)) {
            for(var j = 0; j < document.images.length; j++) {
                var img = document.images[j]
                var imgName = img.src.toUpperCase()
                if(imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText
                    if(img.align == "left") imgStyle = "float:left;" + imgStyle
                    if(img.align == "right") imgStyle = "float:right;" + imgStyle
                    if(img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle +
                        " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" +
                        "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" +
                        "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                    img.outerHTML = strNewHTML
                    j = j - 1
                }
            }
        }
    }
    function addHandler (element, type, handler) {
        if (element.addEventListener) { // DOM2級 事件處理程序,this 指向元素本身。按照添加的順序正向執行
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent) { // IE 事件處理程序,this 指向 window。按照添加的順序反向執行
            element.attachEvent("on" + type, handler);
        } else { // DOM0級 事件處理程序。只能綁定一個事件處理程序
            element["on" + type] = handler;
        }
    }
    addHandler(window,'load',correctPNG)
</script>

將這個js引入項目就可以了

4.ie6下的display:inline-block異常問題

4.1表現

本是inline的html元素設置爲inline-block後,會具有inline-block的特性;
但本是block的html元素設置爲inline-block後,會出現不在同行排布的情況;

4.2上代碼

<style type="text/css">
    .span-inline-block {
        background-color: #7FFFD4;
        display: inline-block;
        width: 100px;
        height: 100px;
        margin: 5px;
    }

    .div-inline-block {
        background-color: #00ff00;
        display: inline-block;
        width: 100px;
        height: 100px;
        margin: 5px;
    }
</style>
<span class="span-inline-block"></span>
<span class="span-inline-block"></span>
<span class="span-inline-block"></span>
<span class="span-inline-block"></span>
<div class="div-inline-block"></div>
<div class="div-inline-block"></div>
<div class="div-inline-block"></div>
<div class="div-inline-block"></div>

4.3上圖

1.谷歌

2.ie6

4.4怎麼搞?

1.若無特殊要求,可以把div改爲span
2.可以設置float屬性。如float爲right時,效果如下

5.ie6下min-height和min-width失效

5.1上代碼

<style type="text/css">
    .min-div-class {
        min-width: 200px;
        min-height: 200px;
        background-color: #00FF00;
    }
</style>
<div class="min-div-class"></div>

5.2上對比圖

1.谷歌

2.ie6(沒錯,這是一張空白的圖)

5.3 怎嘛整?

直接設置width、height。

6.ie6下的select寧死不從╥﹏╥…軟硬不吃!ヘ(;´Д`ヘ)

6.1what?

本來我把select框的樣式給調的美美的,比如這樣

結果在ie6上亂了套,源碼我就不寫了,直接寫demo

6.2 demo!我的代碼!!!

<style type="text/css">
    .selectArea{
        position: relative;
        width:100px;
        height:24px;
        line-height:20px;
        padding-left: 5px;
        border:1px  solid #0079cc;
        overflow: hidden;
    }
    .testSelect{
        width:150px;
        line-height:30px;
        margin: -3px 0px;
        border:0px solid transparent;
        outline: none;
        background: none;
        appearance: none;
        -moz-appearance: none;
        -webkit-appearance:none;
    }
    .dropdown{
        position: absolute;
        right:5px;
        top:10px;
    }
</style>
<div class="selectArea">
    <select class="testSelect">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
    <img class="dropdown" src="https://jhcan333.github.io/can-Share/image/ie6/arrow.png">
</div>

6.3各個瀏覽器展示

谷歌
ie8
ie6

6.4有木有發現ie6下select不聽話?

高度~邊框 ~~~ 完全不好整 ~~~

6.5如何解決?

Ie6上看起來整齊就好了,不要什麼花裏胡哨的東西了~hash走起! (T_T)

<style type="text/css">
    .selectArea {
        position: relative;
        width: 100px;
        height: 24px;
        line-height: 20px;
        padding-left: 5px;
        border: 1px solid #0079cc;
        overflow: hidden;
        _border: 0px #fff solid;
        _padding: 0px;
        _overflow: auto;
    }

    .testSelect {
        width: 150px;
        line-height: 30px;
        margin: -3px 0px;
        border: 0px solid transparent;
        outline: none;
        background: none;
        appearance: none;
        -moz-appearance: none;
        -webkit-appearance: none;
        _width: 100px;
        _margin: 0px;
    }

    .dropdown {
        position: absolute;
        right: 5px;
        top: 10px;
        _display: none;
    }
</style>

差不多是這個效果了吧~(原生的也還是很整齊的啊)

ie6上的css問題就先整理到這裏了,歡迎大家評論討論
備註:轉載註明出處

最近在搞一個和前端程序員相關的公號,除了技術分享之外,也增加了對於職業發展、生活記錄之類的文章,歡迎大家關注,一起聊天、吐槽,一起努力工作,認真生活!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章