筆記:IE下 jquery的fadeIn與fadeOut方法失效的BUG


BUG1:絕對定位嵌套絕對定位

這個問題遇到過好多次,因為沒有做筆記,所以每次遇到這個問題都要研究半天。好記性不如爛筆頭,這話一點沒錯。

<div id="show_list">
      <div class=""  val="0">
           <div class=" posab" style="top:50px; left:260px;"> <img class="png_bg" src="/uploads/productimage/164613_634717302959421250.png" /></div>  
           <div  class="posab alce"  style="top:200px; width:260px; left:80px;">
                <p class=" f18 l22 alce" style="color:#e99417; margin-top:20px;">測試測試測試</p>
                <p class=" f14 l22 alce" style="color:#e99417;">測試測試測試</p>
                <p class="lk444 f14  l22 alce" style="margin-top:5px;">測試測試測試</p>
           </div>
       </div>       
    <div class=""  val="0">
           <div class=" posab" style="top:50px; left:260px;"> <img class="png_bg" src="/uploads/productimage/164613_634717302959421250.png" /></div>  
           <div  class="posab alce"  style="top:200px; width:260px; left:80px;">
                <p class=" f18 l22 alce" style="color:#e99417; margin-top:20px;">測試測試測試</p>
                <p class=" f14 l22 alce" style="color:#e99417;">測試測試測試</p>
                <p class="lk444 f14  l22 alce" style="margin-top:5px;">測試測試測試</p>
           </div>
       </div>       
<div>

posab  是絕對定位的class

只要把絕對定位換成浮動,就可以實現淡隱淡出的效果了。

<div id="show_list">
      <div class="posab"  val="0">
           <div class="" style="margin-top:50px; float:left; margin-left:260px"> <img class="png_bg" src="/uploads/productimage/164613_634717302959421250.png" /></div>  
           <div  class="alce"  style="margin-top:-200px; width:260px; margin-left:80px; float:left">
                <p class=" f18 l22 alce" style="color:#e99417; margin-top:20px;">測試測試測試</p>
                <p class=" f14 l22 alce" style="color:#e99417;">測試測試測試</p>
                <p class="lk444 f14  l22 alce" style="margin-top:5px;">測試測試測試</p>
           </div>
       </div>       

    <div class="posab"  val="0">
           <div class="" style="margin-top:50px; margin-left:260px;"> <img class="png_bg" src="/uploads/productimage/164613_634717302959421250.png" /></div>  
           <div  class="alce"  style="margin-top:-200px; width:260px; margin-left:80px;float:left">
                <p class=" f18 l22 alce" style="color:#e99417; margin-top:20px;float:left">測試測試測試</p>
                <p class=" f14 l22 alce" style="color:#e99417;">測試測試測試</p>
                <p class="lk444 f14  l22 alce" style="margin-top:5px;">測試測試測試</p>
           </div>
       </div>       

<div>

 具體的位置需要做一些調整

BUG2:父級絕對定位嵌套大於父級尺寸的圖片

還有一種情況,是IE8獨有的BUG,格式如下

 <div class=" posab" style=" top:80px; left:260px;width:550px;height:55px">
  <img class="png_bg" src="image.jpg" />
</div>

 如果圖片的大小超過了div的大小,在IE8下面淡隱淡出效果將會失效

BUG3:

網上還差了一種bug,具體沒有遇到過,現也貼上:

IE6 IE7 IE8 在 position : relative 時fadeIn() fadeOut() bug 解決方案

先看一個例子

<div class="fadein">
    <div>
        <div>I am going to fade in ;</div>
        <div>I am going to fade in ;</div>
    </div>
</div>
$('.fadein').fadeIn();

 

以上代碼基本上在所有主流瀏覽器都可以達到預期效果

但是現實是殘酷的, 大家的html結構當然不會這么簡單。

我們再加一點東東

<style>
.relative{position: relative;}
</style>
<div class="fadein">
    <div class="relative">
        <div>I am going to fade in ;</div>
        <div>I am going to fade in ;</div>
    </div>
</div>
$('.fadein').fadeIn();

這個時候在IE 6 78 就會吭爹的事情出現, 動畫不出現有木有!  直接show出來有木有!

這是臭名昭著的 IE 大bug: 如果fadeIn的元素的子元素有position屬性時 以relative值為最嚴重 有position屬性的元素不會出現fadeIn的效果!

可能的曲線解決方法:

1, 不用position: relative;  這個嘛 有時候可以做到

2,如果1實在做不到, 比如筆者遇到的情況,那就用each()。 你可以一個一個做這個效果, 這個當然是可以做到的, 不過效率太差, 搞不好會掛掉用戶的電腦,比如筆者遇到的情況,有幾十個上百個子元素 這個方法是萬萬使不得滴。。

針對性解決辦法

我們要在使用position:relative 而且不使用each()的情況下達到這個效果,可以嗎?

可以!

既然這是一個bug 那一定就有hack的方法

$('.fadein').css('position', 'relative').fadeIn();

在fadeIn()之前動態的將其position屬性改為relative; 會解決IE7下的這個bug

<style>
.relative{position: relative; filter: inherit}
</style>

在你子元素有position屬性的元素加 filter: inherit; 當前元素的第一層子元素也要加。
這兩條一結合 IE678 的問題就都解決了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM