2010-11-04 43 views
0

對於wordpress主題,當你將它們轉過去時,我會將彩色圖像轉換爲黑白圖像。但是,當你移動鼠標的速度非常快時,圖像會保持黑色,直到你再次瀏覽它們。jQuery:卡住jquery動畫

我該如何解決這個問題,以確保當我不把圖片懸停時,它不會停留在黑色&白色?

鏈接到演示:http://www.infictus.com/wordpress_demo/creafolio/

代碼:

function initImage(obj) 
     { 
     var $newthis = $(obj); 
     if ($.browser.msie) 
     { 
      $newthis = $newthis.desaturateImgFix(); 
     } 
     $newthis.addClass("pair_" + ++paircount); 
     var $cloned = $newthis.clone().attr('id', ''); 
     $cloned.get(0).onmouseover = null; 
     $cloned.insertAfter($newthis).addClass("color").hide(); 
     $newthis = $newthis.desaturate(); 
     $newthis.bind("mouseenter mouseleave", desevent); 
     $cloned.bind("mouseenter mouseleave", desevent); 
     }; 

     function desevent(event) 
     { 
     var classString = new String($(this).attr('class')); 
     var pair = classString.match(/pair_\d+/); 
     // first I try was $("."+pair).toggle() but IE switching very strange... 
     $("."+pair).hide(); 
     if (event.type == 'mouseenter') 
      $("."+pair).filter(":not(.color)").show(); 
     if (event.type == 'mouseleave') 
      $("."+pair).filter(".color").show(); 
     } 
+0

這不是一個WordPress的問題...這是一個jQuery的問題。請相應地更改您的標題。 – EAMann 2010-11-04 19:03:39

+0

哦,是的,對不起 – Christophe 2010-11-04 20:48:15

回答

1

我以前碰到過類似的問題,而且它是由鼠標加一個新的mouseenter /鼠標離開事件之前的jQuery可以處理引起的先前的show()命令。要解決這個問題,最簡單的辦法是增加一個通用的命令,使所有您的彩色圖像,以show()運行你的過濾器,以show/hide顏色和b &W¯¯你正在使用的圖像之前。

所以,基本上,你的代碼改成這樣:

function desevent(event) { 
    var classString = new String($(this).attr('class')), 
     pair = classString.match(/pair_\d+/); 

    $(".color").show(); 
    $(":not(.color)").hide(); 

    if (event.type == 'mouseenter') 
     $("." + pair).filter(".color").hide(); 
     $("." + pair).filter(":not(.color)").show(); 
} 

當的mouseenter或鼠標離開事件觸發,該腳本將首先顯示所有的彩色圖像,並隱藏所有B &白圖像的。然後,如果是一個mouseenter,該腳本將隱藏彩色圖像並顯示圖像。