【问题标题】:centre an absolute rounded div above another absolute position div [duplicate]在另一个绝对位置 div 上方居中绝对圆角 div [重复]
【发布时间】:2019-02-28 05:42:48
【问题描述】:

我正在尝试使用 Css 动画创建一个位置类型的动画,其中 location divline div 上标记的兴趣点上进行动画处理。我的第一个兴趣点是 line div 的中心。但是,下面的代码似乎没有做我想要的。

  top: 116px;
  left: 50%;
  transform: translate(-50%, -50%);

.location {
  position: absolute;
  top: 116px;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 16px solid #f3f3f3;
  border-top: 16px solid #3498db;
  border-bottom: 16px solid #3498db;
  animation: Location 4s linear infinite;
}

@keyframes Location {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.line {
  position: absolute;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
  border: 8px solid #f3f3f3;
  width: 500px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>

<body>
  <div class="location"></div>
  <div class="line"></div>
</body>

</html>

【问题讨论】:

  • 旋转覆盖了居中的平移
  • 当我看到 Dogukan Cavus 的回答时,我意识到了这一点。你如何找到所有这些相似的问题?我什至不会考虑搜索接近标题的任何内容。
  • 这就是为什么我们有重复关闭;) 关闭类似的问题,以便以后搜索。有人可能会找到您的问题,然后会看到另一个问题。
  • 为什么不将它们放在同一个 div 中并绝对定位该父级?
  • 如果它们在同一个 div 中,我是否还需要至少放置其中一个以使它们重叠?

标签: html css


【解决方案1】:

这样的?您还应该在动画中添加translate(-50%,-50%)。否则,它会删除translate(-50%,-50%) 并覆盖它。

.location {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 16px solid #f3f3f3;
  border-top: 16px solid #3498db;
  border-bottom: 16px solid #3498db;
  animation: Location 4s linear infinite;
}

@keyframes Location {
  0% {
    transform:translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform:translate(-50%, -50%) rotate(360deg);
  }
}

.line {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 8px solid #f3f3f3;
  width: 500px;
}
  <div class="location"></div>
  <div class="line"></div>

【讨论】:

  • 感谢您指出这一点:)
猜你喜欢
  • 2016-04-16
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 2011-04-09
  • 2012-01-15
  • 2010-09-20
  • 2015-06-14
  • 1970-01-01
相关资源
最近更新 更多