3

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

Code
  • 83
  • 1
  • 2
  • 10

4 Answers4

0

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.

Hamza Kubba
  • 2,259
  • 14
  • 12
0

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.

Babatunde
  • 81
  • 4
0

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?

Short Answer

CSS transitions.

Community
  • 1
  • 1
kalley
  • 18,072
  • 2
  • 39
  • 36
0

Specify the animation name as a third parameter.

$('#fo').toggleClass('big-blue', 'slow', "easeOutSine");
chiwangc
  • 3,566
  • 16
  • 26
  • 32
Hemanth
  • 177
  • 2
  • 11
  • 1
    The question asks specifically how to do this _without using jQueryUI_, but the parameters used in this answer are exclusive to jQueryUI. – AJPerez Dec 10 '19 at 11:58