How to fade div out, change background image and fade in within a loop?

MeltingDog

I need to create my own image slider by changing the background image of a div. I have done this but want to add a fade in/out transition effect.

So far I have:

    var now = 0;
    var int = self.setInterval("changeBG()", 5000);
    var array = ["[[*HomeImage1]]", "[[*HomeImage2]]", "[[*HomeImage3]]", ];


    function changeBG(){
        now = (now+1) % array.length ;
        $('.heroslide').fadeOut(300).delay(1000).css('background-image', 'url("' + array[now] + '")');
        $('.heroslide').fadeIn(300);

    }

But try as I might I cannot get the transitions and css change to work in the right order.

Can anyone point me in the right direction?

somethinghere

You should try to use a recursive function:

var now = 0;
var int = self.setInterval("changeBG()", 5000);
var array = ["[[*HomeImage1]]", "[[*HomeImage2]]", "[[*HomeImage3]]", ];

function changeBG(){
    now = (now+1) % array.length ;
    // Fade out
    $('.heroslide').animate({opacity:0}, function(){
         // Change, fade in, trigger function after 5 seconds again.
         $('.heroslide')
             .css('background-image', 'url("' + array[now] + '")')
             .animate({opacity:1}, function(){
                 setTimeout(changeBG, 5000);
             });
    });
}

Now just call changeBG() once to start animating!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android change background image with fade in/out animation

How to Fade out background-color of a div

How smoothly change image source with fade-in/fade-out?

Fade in and Fade Out Text in DIV - Remove Loop

Fade out of div background affects other divs on top of background image

Background image fade out in jQuery

How to fade in and fade out text in loop

JQuery Div Fade in and out on change

Hover over div/image to fade out image and have text fade in, but hovering over text removes fade out of image. How to prevent?

How to fade loop background images?

How to fade WPF background image?

How an I make a background image fade out to reveal text on hover?

How to create an image that repeats for a js fade in and out background slideshow?

Fade Out an Image when click on button, change image url and then fade in. How to do it?

Background-Image Fade out with gradient

Fade out on scrolling page - background div

How to change color in fade-in and fade-out on div when updating data with sse

How do you fade in/fade out same div onclick of image using JQuery?

Fade background in/out on scroll

How to Initiate Image Fade Out and Fade In with jQuery Function

on hover and on click how to fade out image and fade in content text

how to refresh a specific div content with fade in & fade out

How can I fade in and then fade out text inside a <div>

How to make a created div in Javascript fade in and fade out?

Fade in background image on load

Fade in background image in flutter

fade div background on load

Fade in and fade out background with bootstrap text carousel

Fade out text on image