jQuery fade elements from one class to another, on hover

Alex picture Alex · Feb 7, 2011 · Viewed 17.5k times · Source

can this be done?

for eg.

.class1{
  background-image:(whatever.jpg)
  color: #fff;
}

.class2{
  background-image:(whatever2.jpg)
  color: #999;
}

can I fade all elements that have class1 to class2 when the mouse is over the element, and back to class1 when mouse is out?

Answer

Sang Suantak picture Sang Suantak · Feb 7, 2011

If you don't want to use a plugin, you can do the following:

$(".class1").hover(
function () {
    $(this).fadeOut(function () {
        $(this).removeClass("class1").addClass("class2").fadeIn('fast');
    });
},
function () {
    $(this).fadeOut(function () {
        $(this).removeClass("class2").addClass("class1").fadeIn('fast');
    });
});