2

How to change div background-color with fadeIn/Out,I only want to fade background color,not background image,Please give me some useful code or solution

ailee
  • 56
  • 1
  • 5

4 Answers4

1

Although only supported by modern browsers you might also consider using CSS transitions to achieve the same effect

HTML

<div class='foobar'></div>

CSS

.foobar {
    /* transition properties */
    transition: background-color 2s;
    -moz-transition: background-color 2s;
    -webkit-transition: background-color 2s;
    -o-transition: background-color 2s;

    /* basic styling & initial background-color */
    background-color:maroon;
    width:100px;
    height:100px;
}

/* Change background color on mouse over */
.foobar:hover {
    background-color:blue;
}

Working example here, http://jsfiddle.net/eRW57/14/

lostsource
  • 21,070
  • 8
  • 66
  • 88
1

with this you can fadeout all div's with id #my-background

var $div = $('#my-background');
  $div.each(function blank(){
  $(this).animate({'backgroundColor': '#fff'},2000);
});
0

You can't fade just the background (color or otherwise) of an element using jQuery's fadeIn/fadeOut.

What you can do is place an additional layer (DIV, etc) with your background color and fade in/out on that.

Instead of something like this:

<div id="my-background"></div>

Use this structure:

<div id="container">
  <div id="my-background"></div>
</div>

CSS

#container
{
 position:relative;
 width:200px;
 height:100px;
 background-image:url(my-background-image.jpg);
}
#my-background
{
 height:100%;
 width:100%;
 background-color:blue;
 position:absolute;
 z-index:99;
}

Then use jQuery's fadeIn/fadeOut methods

JS

jQuery("#my-background").fadeOut();
Matthew
  • 8,183
  • 10
  • 37
  • 65
0
<script>
$(document).ready(function(){
  $("p").toggle(function(){
    $("body").css("background-color","green");},
    function(){
    $("body").css("background-color","red");},
    function(){
    $("body").css("background-color","yellow");}
  );
});
</script>

try it.. that should work fine