I have the following html and javascript (jQuery):
<div class="container-a">
<div class="element">...</div>
<div class="element">...</div>
...
</div>
<div class="container-b">
<div class="element">...</div>
<div class="element">...</div>
...
</div>
<script>
function cycle($container) {
setInterval(function() {
$active = $container.find(':last-child')
$next = $active.prev();
$next.css({opacity:0});
$next.insertAfter($active);
$next.animate({opacity: 1}, 500, function() {
$active.insertBefore($container.find(':first-child'));
});
}, 3500);
}
$(function() {
cycle($('.container-a'));
cycle($('.container-b'));
})
</script>
When I run cycle(..) on just one or the other of .container-a or .container-b things work fine (the elements are faded in one after the next by moving the last element to the beginning of the container after an opacity transition). However, when I run cycle on both as above, then the elements in container-a don't transition properly.
I know that it is because of a closure issue because when I step through the code there are cases when the animation complete function is run and $container is .container-a but $active.parent() and $next.parent() are .container-b. I'm having trouble figuring out why exactly this is the case and how to fix it.