I want to have something like this :
$('#fo').toggleClass('tra', 'slow');
I know that i can achieve this with jQuery UI but i'm looking for a faster and smaller solution because i don't wanna integrate in my site another 20-90 kb
I want to have something like this :
$('#fo').toggleClass('tra', 'slow');
I know that i can achieve this with jQuery UI but i'm looking for a faster and smaller solution because i don't wanna integrate in my site another 20-90 kb
Use .animate()
instead. You'll need to animate the individual styles that you want changed. E.g. .animate( { height: "500px", width: "200px" }, 2000 )
to change the height and width to those values over 2 seconds. jQuery UI lets you derive animations from class changes, pure jQuery doesn't.
Without JQuery UI, you'll probably have to CSS3 transitions/animations that trigger at the right time (see http://www.netmagazine.com/tutorials/getting-animations-trigger-right-time) or just .animate
with JQuery.
You can do something like this (but it's not the cleanest):
function animateClass(el, klass) {
var endCSS = el.toggleClass(klass).css();
var startCSS = el.toggleClass(klass).css();
for ( var p in endCSS ) {
if ( startCSS[p] === endCSS[p] ) {
delete endCSS[p];
}
}
el.animate(endCSS, 'slow', function() {
el.toggleClass(klass).removeAttr('style');
});
};
It also assumes that you aren't going to add any other inline styles. Though I guess you could replace the animate callback with something like this:
for ( var p in endCSS ) {
endCSS[p] = '';
}
el.toggleClass(klass).css(endCSS);
It also uses a plugin that extends the use of $.fn.css
to return all CSS from How can I get list of all element css attributes with jQuery?
CSS transitions.
Specify the animation name as a third parameter.
$('#fo').toggleClass('big-blue', 'slow', "easeOutSine");