4

I have this jfiddle where:

  1. CSS animation starts when the div is on viewport.
  2. I have an unknown number of div with an Icon, and 2 text lines.

What I need:

  1. Each icon animated with a delay, with respect to the next one. In my jsfiddle all icons are animated simultaneously.

  2. The actual program might have 1, 2, or 300 divs with an icon, the solution must work for any number, not only with the 3 items of my jsfiddle example.

  3. I have bootstrap on divs, and with the scroll control, the animation only starts if the div appears on viewport, whilst on a notebook I get displayed 6 icons in a row, on a smartphone only 1.

var $animation_elements = $('.animation-element');
var $window = $(window);

function check_if_in_view() {
    var window_height = $window.height();
    var window_top_position = $window.scrollTop();
    var window_bottom_position = (window_top_position + window_height + 15);

    $.each($animation_elements, function() {
        var $element = $(this);
        var element_height = $element.outerHeight();
        var element_top_position = $element.offset().top;
        var element_bottom_position = (element_top_position + element_height);

        //check to see if this current container is within viewport
        if ((element_bottom_position >= window_top_position) &&
           (element_top_position <= window_bottom_position)) {
              $element.addClass('in-view');
        else {
              $element.removeClass('in-view');
    }
});
$window.on('scroll resize', check_if_in_view);
$window.trigger('scroll'); 
another
  • 3,440
  • 4
  • 27
  • 34
DANIEL
  • 515
  • 7
  • 20
  • Do you want the icons to appear in order, for example, when first one ends, begin the second, when second ends, begin third and so on? – another May 23 '16 at 09:44
  • 1
    hi @Roizpi! first of all tks for your time! not necessarily in order - I was working with a 3s delay between each icon. But - I have bootstrap on divs, and with the scrool control the animation only starts if the div appears on screen - but on a notebook I get 6 icons in a row, and on a celphone I have only 1 icon in a row. Then, the animation ends in different icons, and start again with the scroll.(I dont know if you understand, and if this detail can be a problem..) – DANIEL May 24 '16 at 10:43
  • Could you please upload any image of your cellphone displaying the jfiddle? It would be helpful for the diagnostic. @DANIEL Consider this [solution](https://jsfiddle.net/tysp4t06/) for the aforementioned problem with delays. – another May 25 '16 at 15:09
  • [This](https://jsfiddle.net/9mm3h0sj/) one fixes a small issue – another May 25 '16 at 15:25
  • hi @Roizpi! tks a lot! I read your message yesterday and was testing the solution. I got a small issue and was creating a new jfiddle to show you, but now I see your new message and will test the new fix. I will back soon tksssssss! – DANIEL May 26 '16 at 05:45
  • hi @Roizpi ! I test the last solution on jfiddle and it had the same "bug" that I found in the first solution. - see: https://jsfiddle.net/9mm3h0sj/1/ - I create a lot of new divs to complete the screen. If you scroll up / down or click on "CLICK HERE" to go up and down - the icon starts to appears in a "crazy" begin - but with the correct order.. (sometimes begin with the ICON 10, then 11..12..13..14..15 - the last - then 1..2..3.. - the order is ok. Try again and its stars on ICON15.. then 1, 2,3.. If you wait all icons appears, and scroll up, and down the bug is the same.. a "random" start – DANIEL May 26 '16 at 07:08

1 Answers1

1
  1. The function to set a delayed action in JavaScript is setTimeout(), with it you can set one(in this case) delayed action per element (each iteration). However inside for loops, you cannot access to external variables in realtime inside setTimeout(), so you need to do it through a closure (ref1, ref2):

    (function(delay, $element, savedtimeout) {
         // 'savedtimeout'(alias of timeouts) is accessible.
         // but 'timeouts'(external var) is not accessible in realtime.
         savedtimeout[i][0] = setTimeout(function() {
             //Start animation
             $element.removeClass('paused');
             countInView--;
         }, delay, timeouts);
    })(delay, $element, timeouts);
    

    In order to delete a delayed action for a specific icon, you have to assign its returning ID to a variable, then you can interrupt its execution with the given ID using removeTimeout(ID):

    timeouts[i][0] = setTimeout(); // Returns an unique ID
    clearTimeout(timeouts[i][0]);
    
    • You can check my other post to see how to manage and save a timeout ID for each icon.
    • The function you are using $.each cannot be reset to 0 its index, so it gives much less options for future implementations. I would rather replace with a for loop, taking advantage of its index.
  2. The more you narrow the div width, the more divs your smartphone will be able to display. So you have to prefix the width, otherwise it seems like each div will cover all the width on your device's screen:

    .slide-left {
        width: 150px; /* e.g. */
    }
    

Altogether solution: https://jsfiddle.net/quq2q9cg/

another
  • 3,440
  • 4
  • 27
  • 34
  • hi @Roizpi, again, tks a lot for your time and help! Yes, I'm trying the same - reset the function to zero.. - then, I agree and understand what you said! (i'm not an expert in js) - I test your new solution, but still doing the same issue.. If you scroll down/up with the animation in the 2nd row or create 4, 5 new rows (to spend more time to finish all icons animation and restart) - when you scroll down the first icon still randomly.. or as you said, the pointer is in the last position. I'm testing some ideas here too.. but the issue are Always the same. Only SLIDE-LEFT animation are reset. – DANIEL May 28 '16 at 03:22
  • After a tough research I came across a solution. The previous one, had the propper logic, but the removeInterval section was not really working. – another May 28 '16 at 14:08
  • @DANIEL There you go! Please, consider thumb up the answer if useful, or mark it as accepted if solved your original question. Every time I answer, you are asking for different implementations, that is great, but it makes me editing both your post and mine every time, and this could be endless :-). I will try to solve new doubts via commentaries. Cheers. – another Jun 03 '16 at 16:22
  • @DANIEL Did the solution help you? Cheers. – another Jun 08 '16 at 12:01