I'm trying to do a transition between two backgrounds:
$(document).ready(function() {
// Set first background
$('#bg .bg').backstretch( bg[0]['normal'], {speed: 0} );
$('li').click(function() {
var i = $(this).index();
// Select first bg element
var bg1 = $('#bg .bg:first-child');
// Add second element
$('#bg').append('<div class="bg" />');
var bg2 = $('#bg .bg:nth-of-type(2)');
$(bg2).css('left', '200%');;
// Set second element background
$(bg2).backstretch( bg[1]['normal'], {speed: 0} );
// Move first background out
$(bg1).animate({
'left': '-100%'
}, { duration: 500, queue: false });
// Move second background in
$(bg2).animate({
'left': '0'
}, { duration: 500, queue: false });
});
});
My html for the absolute positioned 100%x100% backgrounds is:
<div id="bg">
<div class="bg"></div>
</div>
bg[x]['normal']
are just strings with paths to images.
Briefly, my code appends a new .bg
div to #bg
and uses backstretch to set the background image. It then proceeds to animate the first background by moving it towards the left while the second background comes in from the right.
The code works just fine appart from the fact that there is a white gap between the two backgrounds during the transition. This is likely because bg1 get animate slightly before bg2.
How can I ensure these two backgrounds start their animation at the exact same moment?
(The queue: false
was taken from an answer to this same question, but it is definitely not working in my case)