2012-09-26 52 views
1

有幾個關於這方面的話題,但他們沒有幫助我的情況。我寫了一個小腳本,以便在盤旋時更改一些社交圖標的圖像源。我試圖添加淡入和淡出效果,所以它不僅僅是一個直接的圖像交換。這是我的腳本:添加淡入淡出效果jQuery圖像替換懸停腳本

$(".social_icons").live('hover', function() { 
    if ($(this).attr("class") == "social_icons") { 
    this.src = this.src.replace("-black","-color"); 
    } else { 
    this.src = this.src.replace("-color","-black"); 
    } 
    $(this).toggleClass("on"); 
}); 

我該如何添加fadeIn/fadeOut效果呢?

回答

1

試試這個

$(".social_icons").live('hover', function() { 
    var $curr = $(this); 
    if ($(this).attr("class") == "social_icons") { 
     $curr.fadeOut('slow', function() { 
      $curr.attr('src', this.src.replace("-black","-color")); 
      $curr.fadeIn('slow'); 
     }); 
    } else { 
     $curr.fadeOut('slow', function() { 
      $curr.attr('src', this.src.replace("-color","-black")); 
      $curr.fadeIn('slow'); 
     }); 
    } 
    $(this).toggleClass("on"); 
}); 
+1

'$(本).fadeIn( '慢')'真的嗎?如果您可以將鼠標懸停在某個元素上,則當然可以看到它。 – undefined

+0

哎呀..錯過了.. @ undefined ..感謝您指出錯誤..檢查更新後的代碼 –

+0

謝謝,這工作得很好,但它實際上讓我意識到,我真的不想使用它。我理想地嘗試讓它淡入顏色版本並再次退出,而不是完全淡出第一張圖像並淡入第二張圖像。我不確定這是可能的,但... – corning