Вопрос

I have the following code:

$(document).ready(function() {

    var bgCounter = 0,
        backgrounds = [
           "images/2.png",
           "images/1.png",
           "images/3.png"
        ];

    function changeBackground()
    {
        bgCounter = (bgCounter+1) % backgrounds.length;
        $('#page').css('background', '#000 url('+backgrounds[bgCounter]+') no-repeat');
        setTimeout(changeBackground,100000);
    }

    changeBackground();
    });

I need to add fade in/out effect, between background changes. Any guidance?

Это было полезно?

Решение 2

Just add this:

$('#page').fadeIn()

before

$('#page').css('background', '#000 url('+backgrounds[bgCounter]+') no-repeat');

And this:

$('#page').fadeOut()

after it..

Другие советы

Try this one:

 $(document).ready(function() {
 var bgCounter = 0,
    backgrounds = [
       "images/2.png",
       "images/1.png",
       "images/3.png"
    ];

 function changeBackground()
 {
    bgCounter = (bgCounter+1) % backgrounds.length;
      $('#page').animate({
     opacity: 0
    }, 0).css({
          'background-image': 'url('+backgrounds[bgCounter]+')'
     }).animate({opacity: 1}, 1000);

 setTimeout(changeBackground,10000);
 }

 changeBackground();
 });

Demo: http://jsfiddle.net/nq7uE/8/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top