tags:

views:

40

answers:

3

Is it possible to animate the replacement of a class in javascript?

Let's consider I have this:

.class1 {background-color:blue;}  
.class2 {background-color:red;}  

Is there any Javascript library that can ease the change between the two classes? That wouldn't make the user's computer explode?

If not, what would be a good way of achieving that? A server-generated Javascript file?

+1  A: 

If you can use jQuery it is having animate function which will allow you to animate from class1 to class2.

here is a link

http://api.jquery.com/animate/ for normal animations. Use jQuery UI for animation between classes as suggested in another answers.

sushil bharwani
True but you also need jQuery UI.
Pointy
jQuery's `.animate()` method alone can't animate the replacement of a class.
patrick dw
thanks i realized that .. Edited my answer.
sushil bharwani
+4  A: 

Yes, jQuery can do this when you have jQueryUI loaded as well.

See the demo here: http://jqueryui.com/demos/addClass/

Here's an example specific to your CSS. http://jsfiddle.net/hhEpT/

div { width: 100px; height: 100px; }
.class1 {background-color:blue;}
.class2 {background-color:red;}​

<div class='class1'></div>

$('div').addClass('class2', 1000);
patrick dw
I don't know about you, but this is one of those jQuery UI features that I keep in my personal "completely crazy; avoid" file. It's a feature that works when it works, but when it doesn't stuff just gets weird. However it does in fact exist :-)
Pointy
@Pointy - Agreed. The concept is great, but in practice if I was to use it, it would only be in the simplest of cases. Probably safer to cache the styles in a variable, and use that with `.animate()` instead.
patrick dw
I realize this isn't always practical (especially if your CSS is more involved than just background-colors), but you could also layer `<div class="class1" />` on top of `<div class="class2" />` and simply `.fadeOut()` on hover or another event.
peterjmag
@peter - That could certainly be a possibility in some cases. :o)
patrick dw
It's not the point, really, I did want to have a system that can deal with major modifications within a CSS class. What I did realize, though, is that I'll probably never have to animate anything else than position, size, or color. Anything else would be a little flaky that I wouldn't be very much surprised...
MrZombie
A: 

You can use a combination of jQuery and jQueryUI.

Let's say for example you have a paragraph like this:

<p id="notice" class="class1">This is something to highlight</p>

So basically you want to animate the replace of class1 with class2. Since CSS allows you to override styles, you can simply add a new class and it will override the initial effect.

$("#notice").addClass("class2", 50);

Where 50 is obviously how long the animation takes. For this to work you need to reference both the jQuery and jQueryUI libraries.

The alternative is to use the jQuery animate method. Basically you specify the css that drives the animation, the duration, and a callback.

$('#clickme').click(function() {
  $('#notice').animate(
      {background-color: yellow}, 
      5000, 
      function() {
        $("#notice").removeClass("class1");
      });
});
Jaco Pretorius