I'm a beginner in jQuery and I'm trying to get my fade between DIVs to work as I want. However, I'm running into problems. I want the script to completely refresh the content I'm loading dynamically into my DIV before it fades. This way I know the content displayed are current and I will avoid any flashes of the content because it's refreshed after the DIV has faded in.
Here is my code;
$(window).load(function(){
var divs = $('.fade');
function fade() {
var current = $('.current');
var currentIndex = divs.index(current),
nextIndex = currentIndex + 1;
if (nextIndex >= divs.length) {
nextIndex = 0;
}
var next = divs.eq(nextIndex);
next.stop().fadeIn(3000, function() {
$('#div1').load('div1.php');
$('#div2').load('div2.php');
$('#div3').load('div3.php');
$('#div4').load('div4.php');
$('#div5').load('div5.php');
$(this).addClass('current');
});
current.stop().fadeOut(3500, function() {
$(this).removeClass('current');
setTimeout(fade, 60000);
});
}
fade();
});
So how can I make the next DIV be refreshed BEFORE the fade into the next DIV?
In jQuery, fadeIn() works like this:
It is the same for $.fadeOut();
So you need to insert all your content and only after that call the
$.fadeIn(3000);
method.It should look something along these lines:
HTML:
Javascript:
Tested this on my Chrome browser. It cycles and updates every div one by one every 5 seconds.
Here's a new JSFiddle: LINK
If this is not exactly what you need, you can work your way from there :)