简体   繁体   English

使用 CSS 使 div 随机移动

[英]Making a div move randomly around using CSS

I'm trying to use CSS to make a red div move around randomly (by assigning random coordinates through a variable called rando).我正在尝试使用 CSS 使红色 div 随机移动(通过一个名为 rando 的变量分配随机坐标)。

CSS CSS

<style> 

div {
  width: 50px;
  height: 50px;
  background-color: red;
  position: relative;
  animation-name: box;
  animation-duration: 10s;
  animation-iteration-count: infinite;
}

@keyframes box {
  0%   {background-color:red; left: rando px; top: rando px;}
  25%  {background-color:red; left: rando px; top: rando px;}
  50%  {background-color:red; left: rando px; top: rando px;}
  75%  {background-color:red; left: rando px; top: rando px;}
  100% {background-color:red; left: rando px; top: rando px;}
}

HTML HTML

<div></div>

I also tried to do something similar but with a JS function which contained the random numbers (below is a separate example):我也尝试做类似的事情,但使用包含随机数的 JS function(下面是一个单独的示例):

CSS CSS

<style> 
//variable is assigned here
:root {
  --rando: Math.floor((Math.random() * 20) + 1);
}
div {
  width: 50px;
  height: 50px;
  background-color: red;
  position: relative;
  animation-name: box;
  animation-duration: 10s;
  animation-iteration-count: infinite;
}

@keyframes box {
  0%   {background-color:red; left: rando px; top: rando px;}
  25%  {background-color:red; left: rando px; top: rando px;}
  50%  {background-color:red; left: rando px; top: rando px;}
  75%  {background-color:red; left: rando px; top: rando px;}
  100% {background-color:red; left: rando px; top: rando px;}
}

HTML HTML

<div></div>

JS JS

<script>
var rando = Math.floor((Math.random() * 20) + 1);
</script>

So far none of these examples are working (the red box does not move).到目前为止,这些示例都不起作用(红色框没有移动)。 I'm still new to using variables in CSS so any help or any information would be appreciated.我对在 CSS 中使用变量还是新手,所以任何帮助或任何信息都将不胜感激。

Thanks谢谢

You can query the root with your JavaScript and adjust the values then.您可以使用 JavaScript 查询根目录,然后调整值。 You can't intermix CSS and JS.您不能混合 CSS 和 JS。
Besides that, you need a better understanding of CSS variables.除此之外,您需要更好地了解 CSS 变量。 To use them you must put it inside the var function, as shown in the code.要使用它们,您必须将其放在var function 中,如代码所示。

 const root = document.querySelector(":root"); // we first get the root element root.style.setProperty("--rando", `${Math.floor(Math.random() * 20) + 1}px`); // inject the CSS with JavaScript
 div { width: 50px; height: 50px; background-color: red; position: relative; animation-name: box; animation-duration: 10s; animation-iteration-count: infinite; } @keyframes box { 0% {background-color:red; left: var(--rando); top: var(--rando);} 25% {background-color:red; left: var(--rando); top: var(--rando);} 50% {background-color:red; left: var(--rando); top: var(--rando);} 75% {background-color:red; left: var(--rando); top: var(--rando);} 100% {background-color:red; left: var(--rando); top: var(--rando);} }
 <div></div>

Obviously this animation does absolutely nothing because you're not making the element move or change in any way, but I hope you get the point.显然,这个 animation 完全没有做任何事情,因为你没有以任何方式移动或改变元素,但我希望你明白这一点。 (If you run the example few times you will realize the change of position in the red box.) (如果你多次运行这个例子,你会发现红框中的 position 的变化。)

To answer the "move around randomly" part of the question, you can create multiple "rando" variables and use them:要回答问题的“随机移动”部分,您可以创建多个“随机”变量并使用它们:

 const root = document.querySelector(":root"); // we first get the root element for(let i = 0; i < 10; i++) { root.style.setProperty(`--rando${i}`, `${Math.floor(Math.random() * 200) + 1}px`); }
 div { width: 50px; height: 50px; background-color: red; position: relative; animation-name: box; animation-duration: 10s; animation-iteration-count: infinite; } @keyframes box { 0% {background-color:red; left: var(--rando0); top: var(--rando1);} 25% {background-color:red; left: var(--rando2); top: var(--rando3);} 50% {background-color:red; left: var(--rando4); top: var(--rando5);} 75% {background-color:red; left: var(--rando6); top: var(--rando7);} 100% {background-color:red; left: var(--rando8); top: var(--rando9);} }
 <div></div>
(I've changed 20px to 200px so you can actually see the animation difference.) (我已将 20px 更改为 200px,因此您实际上可以看到 animation 的差异。)

For better luck on your CSS variable venture next time I highly suggest reading the docs and how to change them with JavaScript .为了下次您的 CSS 变量风险获得更好的运气,我强烈建议您阅读文档以及如何使用 JavaScript 更改它们

You can not do this purely with CSS.您不能仅使用 CSS 来做到这一点。 You will probably need Javascript to edit the CSS.您可能需要 Javascript 来编辑 CSS。 In Javascript, repeat在 Javascript 中,重复

<your element>.style.top = Math.random() * (max - min) + min;
<your element>.style.left = Math.random() * (max - min) + min;

over time to randomize the position of your element.随着时间的推移,随机化元素的 position。 Ensure that you have position: absolute;确保您有position: absolute; set in the CSS of your element.在元素的 CSS 中设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM