BrunoWB
BrunoWB

Reputation: 135

fade in background jquery

I'm trying to fade some images on the background of my site but isn't working T_T . Thanks in advance !

HTML :

<body>
    <div id="menu1"></div>
    <div id="menu2"></div>
    <div id="menu3"></div>
</body>

CSS :

body {
    background-color: #1f304e;
    background-image: url('images/bg.jpg');
    background-repeat: no-repeat; 
    background-attachment:fixed;
    background-position: center 0px;
}

JQuery :

$('#menu1').click(function(){
    $(body).animate({background : url('images/bg1.jpg') }, 600);
}); 
$('#menu2').click(function(){
    $(body).animate({background : url('images/bg2.jpg') }, 600);
}); 
$('#menu3').click(function(){
    $(body).animate({background : url('images/bg3.jpg') }, 600);
});

Upvotes: 2

Views: 28724

Answers (2)

Bhaumik Mehta
Bhaumik Mehta

Reputation: 375

HTML

<div class="main"></div>

css

.main { 
    -webkit-transition: all 1s ease;
    -moz-transition: all 1s ease;  
    -o-transition: all 1s ease; 
    transition: all 1s ease;  
    background-image: url(imagesrc);
    background-size: cover;
    background-repeat: no-repeat;
}

JS

setTimeout(function() {

    $(".main").css("background-image", "url(imageSrc)");
}, 2000);

If you want to change background with multiple images then,

var backgroundPath = "images/";
var backgrounds = ["image1.png", "image2.png", "image3.png", "image4.png"];
var i = 0;

setTimeout(function() {

   var timer = setInterval(function() {

       $(".main").css("background-image", "url(" + backgroundPath + backgrounds[i] + ")");

       i ++;

       if (i >= backgrounds.length) {

           i = 0;
       }

   }, 2000);

 }, 3000);

Change the css and js according to your requirements. Cheers!!

Upvotes: 4

compid
compid

Reputation: 1323

You cannot directly animate the background image property of an element. You can fade in an entire element though, so try to create a div that contains the image, and fade that in.

Try to mimic the background with a div instead:

CSS:

#bg {
  background-color: #1f304e;
  background-image: url('images/bg.jpg');
  background-repeat: no-repeat; 
  background-attachment:fixed;
  background-position: center 0px;
  position: fixed;
  width: 100%;
  height: 100%;
  z-index: -1;
}

HTML:

<body>
  <div id="bg"></div>
  <div id="menu1"></div>
  <div id="menu2"></div>
  <div id="menu3"></div>
</body>

Javascript:

$('#menu1').click(function(){
  $("#bg").fadeOut(function() {
    $("#bg").css({background : url('images/bg1.jpg') });
    $("#bg").fadeIn(300);
  }, 300);
}); 
$('#menu2').click(function(){
  $("#bg").fadeOut(function() {
    $("#bg").css({background : url('images/bg2.jpg') });
    $("#bg").fadeIn(300);
  }, 300);
}); 
$('#menu3').click(function(){
  $("#bg").fadeOut(function() {
    $("#bg").css({background : url('images/bg3.jpg') });
    $("#bg").fadeIn(300);
  }, 300);
}); 

This will fade out the background, swap the image, then fade it back in. If you want a proper crossfade, you will need at least two divs in the background.

Upvotes: 4

Related Questions