这是我的代码:
var amount = Math.floor(Math.random() * 101).length; //???
var x = Math.floor(Math.random() * 101) + "vw";
var y = Math.floor(Math.random() * 101) + "vh";
$('<div></div>').appendTo('html').css({
"top": x,
"left": y
});
div {
background-color: blue;
width: 200px;
height: 200px;
position: fixed;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
所以一般来说,我想生成随机位置的随机数量的 div。
不幸的是,this 或 this 文章对我没有帮助。太具体了。
非常感谢您的帮助!
答案 0 :(得分:1)
删除第一行中的 .length:数量现在是一些数字。
然后循环:
for(let i = 0; i < amount; i++)
{
//do the thing with the divs here
}
答案 1 :(得分:0)
我想这就是你的意思? (我把它们弄得更小,这样你就可以更好地看到随机性)
const someRandom = () => Math.floor(Math.random() * 101)
const randomColor = () => `#${Math.floor(Math.random()*16777215).toString(16)}`
for(i = 0; i <= someRandom(); i++) {
$('<div></div>').appendTo('html').css({
"top": someRandom() + "vw",
"left": someRandom() + "vh",
"background-color": randomColor()
});
}
div {
background-color: blue;
width: 10px;
height: 10px;
position: fixed;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>