How to avoid animation of elements that will not fit in the width of the screen?

Asked

Viewed 230 times

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:

Bolas animadas

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.

2 answers

4


Just modify the CSS by removing float and aligning vertically to the top, updated example: http://jsfiddle.net/BzuR2/1/

#balls > img {
    display: inline-block;
    margin-left:28px;
    vertical-align: top;
}

2

Here is a suggestion:

var wrapperWidth = $('#balls').width();
$('#balls img').each(function(){
    $(this).css({
        "top"     : "-" + $(this).height() + "px",
        "left"    : Math.floor(Math.random() * wrapperWidth) + "px"
    });
});

CSS:

#balls > img {
    margin-left:28px;
    position: absolute;
    opacity: 0;
/* removi  float:left; */
}

Example

What I have changed:

  • I removed the opacity: 0; in jQuery, better use in CSS for the balls to be hidden from the start (jQuery can take a while to run and thus avoid the balls to appear suddenly)
  • added a random positioning of the balls with Math.floor(Math.random() * wrapperWidth) + "px"
  • added a position: absolute; for Animate:top to work.
  • This very interesting, only goes wrong with the left position in random because many balls tend to appear practically in the same place! + 1

  • @Zuul, I made a change to spread the balls better. My answer is the only one that uses the animation of the falling balls. If you don’t want that excitement then why do you have it "top": "-" + $(this).height() + "px", to make a 0 ?

  • Insightful, you noticed that little detail ;) Yes, this CSS definition doesn’t make much sense to the problem at hand, but it’s part of the following animation that balls take... in the final job, if you’re more than 1 minute with the postcard open, the balls start shaking (top/bottom) small pixels.

  • ...I’ve already voted in favour here, but I’d like to take that idea of yours and explore it a little bit further by asking a question to solve the question of balls cheering up on existing balls, may I? And I can also count on your answer to that new question?

  • @Zuul, yes of course.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.