Enigme de renversement de Tigerstipe

J’ai donc une liste de divs qui ont des couleurs de fond alternées via jQuery. Ceux-ci ont également des survols qui changent la couleur de fond. Le problème est que la fonction de basculement ne me permet que d’animer cette classe à une couleur d’arrière-plan au passage de la souris, mais comme je l’ai déjà dit, j’ai une couleur d’arrière-plan alternée. Comment puis-je gérer cela dans jQuery? Mon code ci-dessous était ma tentative de déclaration if, else avec pairs et impairs, mais je ne connais pas la syntaxe appropriée.

$(document).ready(function() { $('.whycapad_staff:even').css({"background-color":"#eaebeb"}); $('.whycapad_staff').hover(function() { $(this).stop().animate({"background-color":"#5facf8"}, 300); }, function(){ if ($(this = ':even')){ $(this).stop().animate({"background-color":"#eaebeb"}, 300) }; else { $(this).stop().animate({"background-color":"#FFFFFF"}, 300) } }) }) 

Il suffit d’utiliser css:

 .whycapad_staff:nth-child(even) { background-color:#eaebeb; } .whycapad_staff:hover { background-color:#5facf8; } 

Démo: http://jsfiddle.net/maniator/npWnm/

Voici un exemple si vous voulez juste utiliser jQuery: http://jsfiddle.net/maniator/npWnm/5/

 $(function() { //jQuery fallback $('.whycapad_staff').hover(function() { $(this).data('b-color', $(this).css('background-color')); $(this).css('background-color', '#5facf8'); }, function() { $(this).css('background-color', $(this).data('b-color')); }); }); 

Retour complet: http://jsfiddle.net/maniator/npWnm/9/

 $(function() { //jQuery fallback $('.whycapad_staff').each(function(index, item){ if(index%2 == 1){ $(this).css('background-color', '#eaebeb'); } }); $('.whycapad_staff').hover(function() { $(this).data('b-color', $(this).css('background-color')); $(this).css('background-color', '#5facf8'); }, function() { $(this).css('background-color', $(this).data('b-color')); }); });