tags:

views:

73

answers:

3

This script sets a random color to the body and a div and should just fade between them:

function randcolor(){
 return ('#' + (~~(Math.random() * 16777215)).toString(16))
}

var $body = $("body"),
    $div = $("<div style='display:none; position: absolute; top:0; right:0; bottom:0; right:0;'></div>")

function havefun(){
    $body.css("background", randcolor()).append($div);
    $div.fadeIn(1000);

    setTimeout(function(){
        $div.fadeOut(500, function(){ $div.remove().css("background", randcolor()).hide(); havefun(); })
    }, 1500);
}

havefun()

but there is no fade my color change happens instantly. Can anyone help? here you can see a working example.

A: 

You need jQuery color plugin to fade background colors.

Sarfraz
Not necessarily, the `.animate()` method ought to work.
MvanGeest
I don't want a plugin i want a answer to my question
meo
this is the answer to your question.
Achilles
That's the limitation of `fadeIn` and `fadeOut`, you can not use them to fade colors, you need that plugin and that is the answer i know of.
Sarfraz
cant just someone read my code and tell me whats wrong. i don't need to fade between two colors. I need to fade between the body and a div just like the script in my example does.
meo
I explained this in my answer. "Why doesn't my code work?" is like asking "Why doesn't a lump of coal work as a light bulb element?" It just doesn't.
Achilles
i know you cant fadeIn and FadeOut colors. You dont understand my code, its not what i am tiring to do.
meo
A: 

The reason you can't animate the color fade "natively" without a plug-In is that JQuery can't incrementally adjust the value of the targeted css property on element. ANY CSS property that has a unit in hex or some other non integer/decimal encoding will have the exact same problem. JQuery is simply decrementing/incrementing values to achieve the fade effects and animations. Use the JQuery color plugIn as suggested by Sarfraz.

Edit:

You still are missing the point...the fade effect is predicated on being able to adjust certain CSS properties. Most likely you changing the color is conflicting with the effects attempts to change opacity and other CSS properties.

Achilles
i don't fade the colors i fade a div. my color change is working
meo
+1  A: 

If you just wanted to fade between the divs and the background, what about http://jsfiddle.net/K2BF3/? I messed up the color change code and a timeout, but the fade seems to be working...

Update: I'm not sure what I'm doing myself (why all this recursion-like calling if there's setInterval?), but http://jsfiddle.net/K2BF3/3/ displays even better fades.

Update: I know messing around incorrectly with jsfiddle, but http://jsfiddle.net/KLN24/ is finally a smooth transition.

MvanGeest
meo
Not sure, but if the real application is time-based, I'd use `setInterval()` to make things cleaner. Oh, and I guess your code didn't work because you removed/added the `div` again and again using `.remove()` and `.append()`, which doesn't help in overlaying...
MvanGeest
ah te script is working i have made an error placing the div. i have set right twice but no left. But still it looks a little laggy
meo
Did you see the latest addition to the answer? By the way, the *really* good way to do this is using queues, I think. Look up `.queue()` for more info. Queues can also be delayed using `.delay()`.
MvanGeest