将CSS样式的正文颜色更改为Div

wj8zmpe1  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(125)

我是HTML和CSS的新手(学生),我在这里遇到了一个问题。
我想改变身体的背景颜色时,点击一个元素有一个CSS动画。可以吗?谢谢!
我试图将火箭设置为按钮,但它只改变了火箭的颜色。我想让它改变身体的背景颜色。

body {
  background: radial-gradient(circle at bottom, navy 0, black 100%);
  height: 100vh;
  overflow: hidden;
}

.orbit {
  height: 450px;
  width: 650px;
  border-radius: 50%;
  position: absolute;
  margin: auto;
  left: 500px;
  bottom: 300px;
  animation: spin 5s infinite linear;
}

@keyframes spin {
  100% {
    transform: rotate(360deg)
  }
}

.rocket {
  background-color: #fafcf7;
  height: 50px;
  width: 25px;
  border-radius: 50% 50% 0 0;
  position: relative;
  left: 11px;
  top: 115px;
}

.rocket:before {
  position: absolute;
  content: "";
  background-color: #39beff;
  height: 20px;
  width: 55px;
  z-index: -1;
  border-radius: 50% 50% 0 0;
  right: -15px;
  bottom: 0;
}

.rocket:after {
  position: absolute;
  content: "";
  background-color: #39beff;
  height: 4px;
  width: 15px;
  border-radius: 0 0 2px 2px;
  bottom: -4px;
  left: 4.3px;
}

.window {
  position: relative;
  height: 10px;
  width: 10px;
  background-color: #151845;
  border: 2px solid #b8d2ec;
  border-radius: 50%;
  top: 15px;
  left: 5px;
}
<div class="orbit">
  <button class="rocket"></button>
  <div class="window"></div>
</div>
9udxz4iz

9udxz4iz1#

要做到这一点,你需要了解JavaScript。拿着这个示例代码,尝试自己做更多的事情。

const rocket = document.getElementById("rocket")

rocket.addEventListener('click', (e) => {
  const randomColor = Math.floor(Math.random()*16777215).toString(16);
  document.body.style = `background: radial-gradient(circle at bottom, #${randomColor} 0, black 100%);`
})
body {
  background: radial-gradient(circle at bottom, navy 0, black 100%);
  height: 100vh;
  overflow: hidden;
}

.orbit {
  height: 450px;
  width: 650px;
  border-radius: 50%;
  position: absolute;
  margin: auto;
  left: 500px;
  bottom: 300px;
  animation: spin 10s infinite linear;
}

@keyframes spin {
  100% {
    transform: rotate(360deg)
  }
}

.rocket {
  background-color: #fafcf7;
  height: 50px;
  width: 25px;
  border-radius: 50% 50% 0 0;
  position: relative;
  left: 11px;
  top: 115px;
  cursor: pointer;
}

.rocket:before {
  position: absolute;
  content: "";
  background-color: #39beff;
  height: 20px;
  width: 55px;
  z-index: -1;
  border-radius: 50% 50% 0 0;
  right: -15px;
  bottom: 0;
}

.rocket:after {
  position: absolute;
  content: "";
  background-color: #39beff;
  height: 4px;
  width: 15px;
  border-radius: 0 0 2px 2px;
  bottom: -4px;
  left: 4.3px;
}

.window {
  position: relative;
  height: 10px;
  width: 10px;
  background-color: #151845;
  border: 2px solid #b8d2ec;
  border-radius: 50%;
  top: 15px;
  left: 5px;
}
<div class="orbit">
  <button id="rocket" class="rocket"></button>
  <div class="window"></div>
</div>

相关问题