jeudi 29 octobre 2015

Creating specific images and placing them in random positions within a div

Using the code submitted here, how could I do a similar thing but with a specific number of specific images, with the only random variable being their positions?

$(document).ready(function(){
var ticket="<div class='ticket'><img src='http://ift.tt/1GysLg0'></div>";
var numTickets=10;
for(var x=1;x<=numTickets;x++){
    $(ticket).appendTo("body");
}
// get window dimentions
var ww = $(window).width();
var wh = $(window).height();
$(".ticket").each(function(i){
    var posx = Math.round(Math.random() * ww)-20;
    var posy = Math.round(Math.random() * wh)-20;
    $(this).css("top", posy + "px").css("left", posx + "px")
});
});

I edited 2pha's code to get rid of rotation, since that's what I'm after, but I don't know I'd edit it to generate a specific set of images.

Here's an edited Fiddle to show what I'm talking about

Thanks a lot :)




Aucun commentaire:

Enregistrer un commentaire