mardi 19 juillet 2016

How to add multiple images in sequence and random position

I'm trying to add multiple images to a div in random positions and sequentially (one after the other). I'd like this to happen in loop.

I have the following code which position the images randomly. I'd like the images to show one by one when the page load and in loop until they cover the all div.

I hope anyone can help!

$(document).ready(function() {
  var bodyWidth = document.body.clientWidth
  var bodyHeight = document.body.clientHeight;

  $('.random').each(function(idx, img) {
    var randPosX = Math.floor((Math.random() * bodyWidth));
    var randPosY = Math.floor((Math.random() * bodyHeight));
    console.log(randPosY);
    $(img).css('left', randPosX);
    $(img).css('top', randPosY);

  });
});
body {
  height: 300px;
}
.random {
  position: absolute;
}
<script src="http://ift.tt/1oMJErh"></script>



<img class="random" src="http://ift.tt/LQF7VE">
<img class="random" src="http://ift.tt/12YZuk8">
<img class="random" src="http://ift.tt/25U2zYN">
<img class="random" src="http://ift.tt/1ydQ84e">
<img class="random" src="http://ift.tt/1ydQ84e">



Aucun commentaire:

Enregistrer un commentaire