好吧,我有這樣的代碼,其中每2秒將正文中div的數量加倍到body中。但是我希望這個div位於視口內的一個隨機位置,我可以如何實現它?只有在我的代碼中的第一個div實際上是在隨機位置,每次休息只是疊加起來,離開這裏的視顯示是的jsfiddle:將div添加到窗口內的隨機位置
<!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>
Thnak你現在唯一的問題是,它只是在一個時間不是雙頁面上的div量增加一個格但我確定我能得到它與您的代碼一起工作我appriciate您的幫助! –
沒問題。祝你好運! –