Did you find this thread in search results for "cycle" and "gsap"? I did.
I know this thread is super old, but I've been working on a similar problem recently that requires a solve for what Carl and garyw were mentioning just above.
We are entering the age of HTML5 Banners, and this topic is applicable as a foundation for cycling HTML5 banners.
The challenge is to continously loop (crossfading, in this case) through an array of images, and when you reach the end, have the last element crossfade back to the first element seamlessly and then repeat...
My solution uses plain javascript to 'cut' the first item from that array (making the rest of the array shift left one space) and then 'paste' that first item at the end of the array.
Repeatedly crossfading only the first two items in the array ( 0 and 1 ), then doing the cut/paste to the array results in a continuous cycle that never has to 'restart' between the last item and first items...because it never reaches the end!
Setup CSS:
#imgcontainer {
position:relative;
}
#imgcontainer img {
position:absolute;
top: 0;
left: 0;
opacity: 0;
}
#imgcontainer img:first-child {
opacity:1;
}
This css sets up a container (#imgcontainer) to hold all the images, makes the images stack in front of each other, and makes them all transparent except the first one.
Javascript:
var imgs = $.makeArray( $('#imgcontainer img') )
function crossfade(){
TweenMax.to(imgs[0], 0.5, {css:{autoAlpha:0}})
TweenMax.to(imgs[1], 0.5, {css:{autoAlpha:1}})
imgs.push( imgs.shift() )
}
var cycle = setInterval(crossfade,3000)
The first line uses jquery's makeArray to create an array of references to all the images inside the designated container. If you aren't using jquery, this can be done by defining a traditional javascript array of individual document element references for each of the images.
Next, we define a crossfade function. Inside the crossfade function, two TweenMax calls are used to fade out the first image (which would be the one that is initally opaque), while fading in the second one. They happen synchronously. You can add a small delay to the fade out call to make sure the new image is almost showing before the old one disappears, or you can adjust the speed of the two animations to your liking.
At the end of the crossfade function, the array is cycled left using the javascript push() and shift() methods. The push() method "pastes" something new onto the end of the array. And in this case, that something is the first element of the array which we have "cut" from the beginning using the shift() method. I have found this array push/shift combo to be useful in games where you want to seamlessly cycle through a sequence of elements multiple times.
Finally, the 'crossfade' function is set to repeat every 3 seconds using setInterval(). Each call represents an individual 'slide' in the slideshow or 'frame' in the banner.
GSAP HTML5 BANNER LOOPING:
Here's how the above Javascript would look if you wanted to limit the number of 'loops' of the whole sequence:
var framesShown = 0
var loops = 3 // limit to three loops of all the frames
var imgs = $.makeArray( $('#imgcontainer img') )
var framesLimit = imgs.length * loops
function crossfade(){
TweenMax.to(imgs[0], 0.5, {css:{autoAlpha:0}})
TweenMax.to(imgs[1], 0.5, {css:{autoAlpha:1}})
imgs.push( imgs.shift() )
framesShown++
if (frameShown == framesLimit){
window.clearInterval(cycle)
}
}
var cycle = setInterval(crossfade,3000)
NOTE: You could set the framesLimit variable to any number so the banner can 'end' on the first or last or a middle frame. In that case just disregard the line where "loops" is defined.