Skip to content

Instantly share code, notes, and snippets.

@omniosi
Created March 6, 2014 22:14
Show Gist options
  • Save omniosi/9400894 to your computer and use it in GitHub Desktop.
Save omniosi/9400894 to your computer and use it in GitHub Desktop.
create randomly positioned copies of an object
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
#box{
width:300px;
height:250px;
background: lightblue;
}
#dot{
display:none;
}
.drop{
width:10px;
height:10px;
background: grey;
position: relative;
}
</style>
</head>
<body>
<div id="box">
<div id="dot" class="drop"></div>
</div>
</body>
</html>
var box = document.getElementById('box'),
dot = document.getElementById('dot');
function getRandomInt(min,max){
return Math.round(Math.random() * (max - min + 1)) + min;
}
for (var i=0; i<300; i++){
var name = "dot"+i;
var drop = dot.cloneNode(true);
drop.id = name;
box.appendChild(drop);
drop.style.top = getRandomInt(0,5) + 'px';
drop.style.left = getRandomInt(0,300) + 'px';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment