好的,所以我有这段代码,其中每 2 秒将正文中的 div 数量加倍附加到正文中。但是我希望这个 div 位于视口内的随机位置,我该如何实现呢?每次其余的只是堆叠并离开视口时,我的代码中只有第一个 div 实际上出现在随机位置,这里是 jsfiddle:
<!doctype html>
<html>
<head>
<title>jQuery Project: An exploding game</title>
<meta charset="utf-8">
<style>
body, html {
width: 960;
height: 500%;
}
div.box {
position: relative;
width: 100px;
height: 100px;
background-color: orange;
}
div.exploding {
position: absolute;
width: 100px;
height: 100px;
background-color: red;
}
</style>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"> </script>
<script>
$(document).ready(function(){
$("body").append("<div class='box'></div>");
// add new div *2
$("div.box").each(function() {
var numRand = Math.floor(Math.random()*501);
$(this).css({'margin-left': numRand});
setInterval(function() {$(".box").clone().appendTo("body");}, 3000);
})
});
</script>
</head>
<body>
</body>
</html>