2013-01-12 169 views
0

我需要在所有動畫完成後從元素中完全刪除不透明樣式。
http://jsfiddle.net/SthJy/1/爲什麼我無法從元素中刪除內聯樣式

我試着設置css('opacity', '')removeAttr('style'),但沒有效果,inilne風格仍然存在。 有人可以幫助我嗎?

+0

您可以用0 –

+0

那麼持續時間動畫回不透明1,有沒有'style'屬性,所以我懷疑是行不通的。但是,爲什麼你需要明確地刪除它? –

+0

我需要刪除它,因爲我有:懸停類,它不與內聯風格工作。 @charlieftl答案解決了它。 – Dharman

回答

2

delay將無法​​工作css()removeAttr(),因爲它們不可排隊。在delay()文檔

您需要使用css()removeAttr()最終動畫

$("#helpCloud").animate({opacity: 1}, 200) 
       .delay(2200) 
       .animate({ opacity: 0 }, 200, function(){ 
         setTimeout(function(){ 
          $(this).removeAttr('style'); 
         }, 300); 
       }); 

完全callack內由於元素的默認不透明度更多是在CSS爲零,不知道你是希望看到

API參考:http://api.jquery.com/delay

+0

通過使用setTimeout()你不同步的jQuery動畫隊列。考慮一下如果你想在它之後添加另一個動畫會發生什麼?使用.queue()或將回調附加到.delay()將解決此問題。 –

+0

@treeFlute這是最後一個動畫,它有什麼不同? – charlietfl

+0

在這裏,你是對的,它沒有改變,但不要打破同步的好習慣。 –

2

通過使用.queue()您保持與動畫同步。

$("#helpCloud").animate({ opacity: 1 }, 200) 
      .delay(2200) 
      .animate({ opacity: 0 }, 200) 
      .delay(300) 
      .queue(function(next) { 
       $(this).css('opacity', ''); 
       alert('happend only after .delay is over'); 
       next() 
      }) 

現場演示:http://jsfiddle.net/SthJy/3/

另一種方式來得到它使用上.dalay回調相同的結果(300,函數(){});

$("#helpCloud").animate({ opacity: 1 }, 200) 
       .delay(2200) 
       .animate({ opacity: 0 }, 200) 
       .delay(300, function() { 
        $(this).css('opacity', ''); 
        alert('happend only after .delay is over'); 
       }); 

現場演示:http://jsfiddle.net/SthJy/4/

相關問題