jQuery fade elements from one class to another, on hover
Asked Answered
S

7

8

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?

Symphonious answered 7/2, 2011 at 10:8 Comment(0)
T
2

If you give both the same absolute position, using fadeIn() and fadeOut() will have this effect (attached to onmouseover and onmouseout).

Testate answered 7/2, 2011 at 10:11 Comment(0)
V
17

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');
    });
});
Venusberg answered 7/2, 2011 at 10:20 Comment(0)
E
15

Have a look at jQuery UI's extension to addClass. It allows a duration parameter to give the possibility of animation.

Here, I think you want to do something like this:

$('.class1').hover(function(){
    $(this).addClass('class2', 800);
}, function(){
    $(this).removeClass('class2', 800);
});

Obviously you'll need to install jQuery UI for this.

Electroencephalogram answered 7/2, 2011 at 10:14 Comment(1)
can it animate background images? on jquery UI there are only a few css properties listed in animationSymphonious
A
4

I think this plugin is what you are looking for. It allows you to animate between classes. For example:

$('.class1').animateToClass('.class2', 1000);
Adman answered 7/2, 2011 at 10:11 Comment(2)
+1 This is also how I understood the question. Note that for color animations you would need either jQuery UI or a plugin like plugins.jquery.com/project/colorCleary
@Alexandra So you downloaded the plugin I linked to. Included it just below where you included jQuery and then used the code I showed as an example in conjunction with something like @Electroencephalogram posted? Because it should work fine, unless you are using jQuery 1.5 which may or may not work with this plugin.Adman
T
2

If you give both the same absolute position, using fadeIn() and fadeOut() will have this effect (attached to onmouseover and onmouseout).

Testate answered 7/2, 2011 at 10:11 Comment(0)
C
1

This is my implementation:

      $(this).fadeOut("fast"); or $(this).hide();
      $(this).removeClass('css1').addClass('css2');
 $(this).fadeIn("slow");
Craniology answered 2/3, 2012 at 16:4 Comment(0)
A
0

By providing Jquery hover() with the handlerIn and handlerOut functions as documented here you can easily handle the mouse in and mouse out events. Next, take a look at Jquery UI switchClass() to easily switch between two classes in a clean way. Lastly, adding fadeIn() makes all of this a clean transition.

$("div.class1").hover(
 function(){$("div").switchClass("class1", "class2").fadeIn(800);},
 function(){$("div").switchClass("class2", "class1").fadeIn(800);}
);

Working CodePen

Auriol answered 29/10, 2018 at 17:10 Comment(0)
J
-2

i think this might be help full to you....

$('.class1').mouseover(function() {
    $(this).toggleClass('class2');
 });
Joanne answered 7/2, 2011 at 10:18 Comment(1)
Using jQuery's toggleClass method toggles one class on and off an element. It has nothing to do with two classes and toggling between them.Rattigan

© 2022 - 2024 — McMap. All rights reserved.