关闭。这个问题需要更多 focused .它目前不接受答案。
想改进这个问题?更新问题,使其仅关注一个问题 editing this post .
1年前关闭。
Improve this question
在下面的 CSS 代码中,我想使用 Javascript 的 Math.random() 属性来使正方形的大小随机化,但我不知道如何使用它来指定 css 中的大小。
#square {
width: 100px;
height: 100px;
background: red;
}
在这里代替指定的宽度和高度,我希望它是随机的。
另外我希望正方形的坐标/方向是随机的。
它不同于生成随机数。生成随机数是解决这个问题的一部分,但不是完整的解决方案。
最佳答案
干得好。工作代码。
var randomWidth = Math.random()* 500;
var width = randomWidth.toString();
document.getElementById("square").style.width = width + "px";
document.getElementById("square").style.height = width + "px";
#square {
background: red;
}
<div id="square"></div>
希望这可以帮助。
关于javascript - 我想使用 Javascript 操作 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62464278/