I am trying to create jQuery color animation without any external plugins. So far i could manage to do this with jQuery-ui but i want to learn this way with pure jQuery animate()
Is is possible to do this with less code and without any external jQuery plugins on 1.8 ?
Here is jsFiddle sample with jQuery 1.7.2 and ui
jQuery:
var Text = $('h1');
var Box = $('.box');
Text.click(function() {
Text.animate({'color':'#f00'},600)
.delay(200).animate({'color':'#ff0'},600)
.delay(200).animate({'color':'#000'},600);
});
Box.click(function() {
Box.animate({'background-color':'#f00'},600)
.delay(200).animate({'background-color':'#ff0'},600)
.delay(200).animate({'background-color':'#000'},600);
});
This cannot be done without plugins or the jquery ui as far as I am aware, certainly not to the same degree of accuracy. Unless of course you are willing to use a large custom written function that would take a fair amount of coding but would produce the same result as a plugin but with more effort and work involved.
There are a number of plugins that can be used, i quite like jquery-color.
Personally I would say that the jquery ui is the best way to accomplish this.
Sorry if this is not the answer you we're hoping for
EDIT:
as gautamdharmapuri has said you could simply change the colours using the css function and a time delay or timeout but in order to achieve an animation effect you would need to work out all the colours in between, and to make it look smooth you would need a lot of transitions.
There is a working demo that I've put together of this idea here http://jsfiddle.net/2EECN/21/ it shows that it would be quite code intensive to actually implement.
jQuery:
The colours were simply taken for a hex colour chart on w3schools in this example. http://www.w3schools.com/cssref/css_colors.asp
IMPROVED ANSWER:
Ok, So this actually intrigued me rather a lot as to how it would be done, and you can actually see with the following example that you gain more control over the cycles the animation can go through if you explicitly set it. http://jsfiddle.net/2EECN/21/.
It also doesn't look half bad! So I would now say that this is most certainly possible and achieveable however you would be better packing it into a small plugin that extends Animates Functionality.
You asked for less code, so here is the relevant code which I minified with Google Closure Compiler:
It is about 7kb in UTF8 without gzip or other compressions, and allows you to animate colors exactly as you were previously doing with jQuery UI. That means, it allows you to remove the jQuery UI while keeping the same code.
Fiddle
I'll also suggest to prioritize your development time more. 7kb on a first load won't be a noticeable change for any end-user, not even dial up ones. That's why I consider refactoring this a waste of time. There's no need to spend hours/days reinventing the wheel.
=]