How to loop in jquery as soon as you load the page?

0

Save yourself! I have a drawing of an arrow, and I would like it to move down and up without stopping, from the moment someone uploaded the page.

$(document).each(function() {

  $('#seta').animate({top: "100px"}, 1000 ),
  $('#seta').animate({top : "-100px" }, 1000);

});

I did it and it seems to work, but it just goes down and up and on and on. I've read the jquery documentation a bit until I get to this .each() but I'm having trouble understanding why it is not working, thank you!

    
asked by anonymous 29.03.2018 / 08:53

1 answer

1
  

The each() you use when you want to cycle through a collection of elements with a   same identifier (a class, tag, attribute, etc.). It does not apply to you.

You can create a function that will get a parameter ( true up, false down) to see if the element will go up or down, callback of the animation will call the function again by switching, creating an looping infinity:

$(document).ready(function(){
   
   (function mover(i){
      
      $('#seta').animate({top: (i ? "-" : "+")+"100px"}, 1000, function(){
         mover(!i);
      });
      
   }())
   
});
#seta{
   position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="seta">
   seta
</div>
    
29.03.2018 / 09:10