Javascript show several div'’s in different time intervals

Asked

Viewed 226 times

1

my question is about how to make various elements appear with different classes and times. The idea is to do as the Facebook Live interaction. The Code is as follows::

<ul class="reactions">
   <li class="size1">:)</li>
   <li class="size2">:o</li>
   <li class="size3">:(</li>
   <li class="size1">:/</li>
   <li class="size2">:-</li>
   <li class="size3">:*</li>
</ul>

By logic, Javascript (or Jquery) would have to print these 6 elements randomly drawing size and "li", I have no idea how to do this. I already have them in html, it will not be necessary to pull the bank. Can help me?

  • 1

    I don’t know exactly what interaction you’re referring to, so you can set an example with an image?

  • Something like this: https://goo.gl/gPAxrX (I refer to higher reactionns) the animation I already have, the <li> are already programmed for when removed from them the diplay:None (because the classes size1, size2 and Size3 have display:block) they already animate and go left. I just need javascript to do this "size" class injection at different times.

  • It’s something like watching emoticons hover on the screen, in the upper section like in the example of this video?

  • Exactly that. My CSS already does this when I change the diplay:None to block, it already takes the li and applies animation, my problem is in the javascript interlink this and apply at different times

1 answer

1


I don’t know if it’s the best way to do it, but it worked:

$(document).ready(function() {
  var ul = $("ul").find("li");
  var rand = function() {
    var randNumber = Math.floor((Math.random() * ul.length) + 0);
    ul.each(function() {
      $(this).hide()
    });
    $(ul[randNumber]).show();
    setTimeout(rand, randNumber * 300);
  }
  rand();


})
<script src='https://code.jquery.com/jquery-3.2.0.min.js'></script>
<ul class="reactions">
  <li hidden class="size1">:)</li>
  <li hidden class="size2">:o</li>
  <li hidden class="size3">:(</li>
  <li hidden class="size1">:/</li>
  <li hidden class="size2">:-</li>
  <li hidden class="size3">:*</li>
</ul>

I created a loop with SetTimeout() with random time, and an array with all li that is varying, taking advantage of the same random number and playing in the index of the array. This number goes from 0 to the length of the array, and within the loop time it is multiplied by 300, but you can create another random number to specify the maximum time as desired.

Example in jsfiddle

  • I sort of took the snippet of your code and put it in what I have here. See that the "ball" needs to go through the entire page and not disappear before the other appears, they have to appear and go through the whole page until the css disappear with it on one side and reappear on the other. http://codepen.io/duarteleme/pen/MpQjZN to view the whole animation change the time interval to 1200

  • It turned out the way you wanted it?

  • Turned Excellent Edson but still not what I need as final format, as you can see codepen.io/duarteleme/pen/Mpqjzn need the "balls" remain on the screen until they disappear, but not one at a time, I need it to appear and do not "disappear" via . Hide(). Just make it appear so I can show several balls appearing at the same time, as on facebook live same goo.Gl/gPAxrX

  • I made a little change in JS that worked: var ul = $(".user");&#xA; var rand = function(){&#xA; var randNumber = Math.floor((Math.random() * ul.length) + 0);&#xA; ul.each(function(){&#xA; if($(this).offset().left < 50)&#xA; $(this).hide()&#xA; });&#xA; $(ul[randNumber]).show();&#xA; setTimeout(rand, randNumber * 900);&#xA; }&#xA; rand();

  • here the link: http://codepen.io/anon/pen/aJqwNK, in the first "shipment" everything goes together, then it goes the way you wanted. I will analyze why this.

  • 1

    Fixed the html, the li must be 'Hidden' right at the beginning, doing this with your initial code is enough, it worked perfectly.

  • Edson, excellent !!!! THANK YOU. Your contribution was very valuable. I’ll leave it in the codepen for those who need it. http://codepen.io/duarteleme/pen/MpQjZN

  • Oops, happy to help, Hug

Show 3 more comments

Browser other questions tagged

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