javascript - 我想使用 Javascript 操作 CSS

标签 javascript css

关闭。这个问题需要更多 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/

相关文章:

html - 自定义单选 CSS 和多行文本换行

javascript - 如何对 Playground 中闪电网络组件 "datatable"的 tds 应用一些简单的样式?

javascript - 使用 async.js mapValues 获取部分结果

javascript - 删除页面中所有样式的最简单方法

javascript - ASP.NET 更改页面时更改 session 值

jquery - 灰色屏幕覆盖登录表单中的打开页面(例如 Mozilla Alert)

javascript - 如何在 Angular 中使用复选框作为选项的下拉菜单?

使用 CSS3 的 HTML5 输入验证图像

javascript - 访问app.js中的io对象

javascript - flex :1 not filling whole screen