Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change CSS using JQuery AFTER animation is done

Tags:

jquery

Can anyone help me solve this little problem with JQuery. I have a div that i keep on changing the margin of it when the mouse hovers over tabs, I also want the color of those tabs to change when the mouse gets over them.

The function is working perfectly fine, but there is one little issue I have, the color of the tab is changing as soon as I get the mouse over it while I want the animation to finish then change the tab.

Here is the code I'm using:

            case 'cat4' : 
                        $('#bg').stop();
                        $('#bg').animate({'marginLeft': '255px'}, 500);
                        $(this).css('color', '#7f3f97');
                        $('#bg').css('background-image', 'url(images/3.jpg)');
                        break;

I want the color to change (on the 3rd line of the code) right after the animation (on the 2nd line) finishes.

Many thanks ...

like image 745
Naruto Avatar asked Jan 24 '11 14:01

Naruto


People also ask

Can we change CSS using jQuery?

It is possible to change the CSS property of an element by using a simple JavaScript API, but we try to complete this challenge using jQuery css() method. Syntax: $(). css(propertyname, value); $().

Is it possible to manipulate all CSS properties with the jQuery animate method?

Is it possible to manipulate ALL CSS properties with the animate() method? Yes, almost!

Which jQuery method can be used to animate changes in CSS properties?

The . animate() method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties.

Can the jQuery animate () method be used to animate any CSS property?

The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).


2 Answers

Instead of chaining them after the .animate call, put those .css calls into .animate's complete callback. Also, you can shorten your solution by passing an object of key/value pairs to .css.

$('#bg').stop().animate({
    'marginLeft': '255px'
}, 500, function() {
    $(this).css({color: '#7f3f97', backgroundImage: 'url(images/3.jpg)'});
});

Additionally, it is less verbose and more manageable to apply your CSS using .addClass:

.newClass{
    color: '#7f3f97'; 
    backgroundImage: 'url(images/3.jpg)'
}

$('#bg').stop().animate({
    'marginLeft': '255px'
}, 500, function() {
    $(this).addClass("newClass");
});
like image 185
karim79 Avatar answered Dec 30 '22 21:12

karim79


You can use the callback of animate():

case 'cat4':
    $('#bg').stop().animate({'marginLeft': '255px'}, 500, function(){
        $(this).css({ color:'#7f3f97', backgroundImage:'url(images/3.jpg)' });
    });

    break;
like image 27
Justin Niessner Avatar answered Dec 30 '22 21:12

Justin Niessner