6
I created an animation in jQuery for a customer’s Christmas card. However, on 1024x768 or 800x600 resolution screens, elements appear in strange places:
The code below hides the balls at startup, then will show up one by one until no more:
// 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
Since the images are all floating on the left, if the last image is not high enough to occupy the entire height of the container, the image that follows 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 full and working example.
Note: If the "bug" is not visible, simply stretch or shrink the preview square width until it is noticed. Since the images are in my Dropbox, the first access to Fiddle is "strange" until the images have been downloaded.
Known solution
The solution that can solve this, is to put all the balls in png with the height of the container where they are being presented, so no more spaces that cause this problem. However, this brings more download load for those who watch the animation, so I would like to avoid this solution. On the other hand, answering the question above, one can avoid animating elements that are not even visible.
"Does this bring more download load" the JPG/PNG compression itself does not make this problem insignificant? After all, it is a large rectangular area where every pixel is transparent... I tested with
bola_pequena_verde.png
and file size changed by less than 10% even after enlarging by 100 times.– mgibsonbr