0

I have a code to change image for every fixed intervals but this image is just normally changing. How can I use fadeout option to my code so that it will slowly fadeout to open a new image. My code follows

$(document).ready(function() {
        (function() {
            var curImgId = 0;
            var numberOfImages = 2;
            window.setInterval(function() {
                $('body').css('background-image','url(bg' + curImgId + '.jpg)');
                curImgId = (curImgId + 1) % numberOfImages;
            }, 15 * 100);
        })();
 });
Karthik Malla
  • 5,570
  • 12
  • 46
  • 89

3 Answers3

1

You cannot fade background images.. You can only fade elements (but fading the body will fade all contents as well)

You will need to add it as an element and put it behind all others.. and fade that element with .fadeIn() and .fadeOut()

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

You can take a look at this thread:
CSS3 background image transition.
It's a nice solution with css.

Community
  • 1
  • 1
pbibergal
  • 2,901
  • 1
  • 17
  • 19
0

You could fade the opacity of the element to zero, then change the background-image, then fade it back to 1:

$(document).ready(function() {
        (function() {
            var curImgId = 0;
            var numberOfImages = 2;
            window.setInterval(function() {
                $('body').fadeTo(500, 0, function(){
                    $(this).css('background-image','url(bg' + curImgId + '.jpg)');
                    $(this).fadeTo(500, 1);
                });
                curImgId = (curImgId + 1) % numberOfImages;
            }, 15 * 100);
        })();
 });
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139