I created a jQuery animation for a customer's Christmas postcard. However, in 1024x768 or 800x600 resolution screens, elements appear in strange locations:
The code below hides the balls at startup, then makes them appear one by one until you can not find them:
// prepare balls
$('#balls img').each(function(){
$(this).css({
"opacity" : 0,
"top" : "-" + $(this).height() + "px"
});
});
// animate balls
function gimeBalls(){
$("#balls img:not('.done'):first").animate({
"opacity" : 1,
"top" : 0
}, 400, function() {
$(this).addClass("done");
});
}
setInterval(gimeBalls, 400);
Problem
As the images are all floated to the left, if the last image is not high enough to occupy the entire height of the container, the following image will look like the screenshot shown above.
Question
How can I with jQuery detect that the element to be displayed next will no longer fit the width of the screen, thus stopping the animation for all other elements?
See JSFiddle with a full example and working.
Note: If the bug is not visible, simply stretch or shrink the width of the preview square until you notice it. As the images are in my dropbox, the first access to the Fiddle is "strange" until the images have been downloaded.
Solution
The solution that can solve this, is to put all the balls in PNG with the height of the container where they are to be presented, thus finishing the spaces that cause this problem. However, this brings more download load to those who see the animation, so I would like to avoid this solution. On the other hand, by answering the question above, you can avoid animating elements that are not even visible.