I have a list of div elements, which is dragable items.
I need to do, that all elements, i will generate random top and left positions, which will be in range of my container, but i dont know how to do it 🙂
Container div is: width: 800px; height: 500px;
My js now is like:
var randomTopPos = Math.floor(Math.random() * (600 / 2));
var randomLeftPos = Math.floor(Math.random() * (5));
$(".t-shirt-design").css({
'top' : randomTopPos,
'left': randomLeftPos
});
Given than your container id is
containerand your draggable id isdragid, you can do as follows:UPDATE:
Updated the code to accomodate several
.t-shirt-designelementsUPDATE 2:
Also you have an error in your html code, an html element can only have ONE id, you have two in your container html element, as follows:
replace it for just one, the correct would be:
UPDATE 3:
Looking at your page, I adjust my code to better fit your requirements (your draggable elements have different widths and heights), so try my update code instead, also better execute this code on the
window.loadevent, instead of.ready, as we need the images to be loaded so the div heights and widths are the correct ones, so replace your following line:for this one: