【问题标题】:Inserting pictures into circles using JavaScript, HTML and CSS使用 JavaScript、HTML 和 CSS 将图片插入圆圈
【发布时间】:2021-08-03 20:26:50
【问题描述】:

来自this question(“使用CSS围绕圆圈旋转对象?”),我复制了以下代码,但是可以在代码中插入图片吗?我想将图片插入圆圈中,以便一张图片围绕另一张图片运行。例如,地球绕太阳运行。

如何修改代码以使地球绕太阳运行而不是空白圆圈?

<!DOCTYPE html>
<html>
<head>
<style>
.outCircle  {
    width: 200px;
    height: 200px;
    background-color: blue;
    left: 270px;
    position: absolute;
    top: 50px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
}



.rotate {
    width: 100%;
    height: 100%;
    -webkit-animation: circle 10s infinite linear;    
   
}

.counterrotate {
    width: 50px;
    height: 50px;
    -webkit-animation: ccircle 10s infinite linear;    
}
    
.inner {
    width: 100px;
    height: 100px;
    background: blue;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 100px;
    position: absolute;
    left: 0px;
    top: 0px;
    background-color: pink;
    display: block;

}


@-webkit-keyframes circle {
    from {-webkit-transform: rotateZ(0deg)}
    to {-webkit-transform: rotateZ(360deg)}
}

@-webkit-keyframes ccircle {
    from {-webkit-transform: rotateZ(360deg)}
    to {-webkit-transform: rotateZ(0deg)}
}
</style>
</head>
<body>
<script>
.outCircle {
  width: 200px;
  height: 200px;
  background-color: blue;
  left: 270px;
  position: absolute;
  top: 50px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
}
.rotate {
  width: 100%;
  height: 100%;
  -webkit-animation: circle 10s infinite linear;
}
.counterrotate {
  width: 50px;
  height: 50px;
  -webkit-animation: circle 10s infinite linear;
}
.inner {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 100px;
  position: absolute;
  left: 0px;
  top: 0px;
  background-color: blue;
  display: block;
}
@-webkit-keyframes circle {
  from {
    -webkit-transform: rotateZ(0deg)
  }
  to {
    -webkit-transform: rotateZ(360deg)
  }
}
@-webkit-keyframes ccircle {
  from {
    -webkit-transform: rotateZ(360deg)
  }
  to {
    -webkit-transform: rotateZ(0deg)
  }
}
</script>

<div class="outCircle">
<div class="rotate">
<div class="counterrotate">
<div class="inner">Hello</div>
</div>
</div>
</div>
</body>
</html> 



编辑: 这是我添加图像时的输出:

<!DOCTYPE html>
<html>
<head>
<style>
.outCircle  {
    background-image: url("https://media.beam.usnews.com/5a/5e/5a739e244b289049e789d7752975/170531-sun-editorial.jpg");
    width: 200px;
    height: 200px;
    background-color: blue;
    left: 270px;
    position: absolute;
    top: 50px;
    -moz-border-radius: 100px;
    -webkit-border-radius: 100px;
    border-radius: 100px;
}



.rotate {
    width: 100%;
    height: 100%;
    -webkit-animation: circle 10s infinite linear;   
   
}

.counterrotate {
    width: 50px;
    height: 50px;
    -webkit-animation: ccircle 10s infinite linear;    
}
    
.inner {
    width: 100px;
    height: 100px;
    background: blue;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 100px;
    position: absolute;
    left: 0px;
    top: 0px;
    background-color: pink;
    display: block;
    background-image: url("https://i.imgur.com/Eo52CF0_d.webp?maxwidth=760&fidelity=grand");

}


@-webkit-keyframes circle {
    from {-webkit-transform: rotateZ(0deg)}
    to {-webkit-transform: rotateZ(360deg)}
}

@-webkit-keyframes ccircle {
    from {-webkit-transform: rotateZ(360deg)}
    to {-webkit-transform: rotateZ(0deg)}
}
</style>
</head>
<body>
<script>
.outCircle {
  width: 200px;
  height: 200px;
  background-color: blue;
  left: 270px;
  position: absolute;
  top: 50px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
}
.rotate {
  width: 100%;
  height: 100%;
  -webkit-animation: circle 10s infinite linear;
}
.counterrotate {
  width: 50px;
  height: 50px;
  -webkit-animation: circle 10s infinite linear;
}
.inner {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 100px;
  position: absolute;
  left: 0px;
  top: 0px;
  background-color: blue;
  display: block;
}
@-webkit-keyframes circle {
  from {
    -webkit-transform: rotateZ(0deg)
  }
  to {
    -webkit-transform: rotateZ(360deg)
  }
}
@-webkit-keyframes ccircle {
  from {
    -webkit-transform: rotateZ(360deg)
  }
  to {
    -webkit-transform: rotateZ(0deg)
  }
}
</script>
<div class="outCircle">
<div class="rotate">
<div class="counterrotate">
<div class="inner">Hello</div>
</div>
</div>
</div>
</body>
</html> 

【问题讨论】:

  • 那个轨道是你真正想要的吗?
  • @AHaworth:是的。

标签: javascript html css


【解决方案1】:

我只是将 background-image css 添加到两个元素中。并将来自维基百科的地球和月球图片链接到该元素。

body{
    background:#000;
}
.earth, .moon{
    background-position: center; /* Center the image */
    background-repeat: no-repeat; /* Do not repeat the image */
    background-size: 120%; /* Resize the background image to cover the entire container */
    -moz-border-radius: 50%; /* to make circle shape */
    -webkit-border-radius: 50%;
    border-radius: 50%;
}
.earth  {
    background-color: blue;
    background-image:url( "https://upload.wikimedia.org/wikipedia/commons/9/90/Small_Earth.jpg" );
    position: absolute;
    width: 200px;
    height: 200px;
    box-shadow:0 0 20px dodgerblue;
    margin:50px;
}
.moon {
    position: relative;
    background-color: white;
    background-image:url( "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/631px-FullMoon2010.jpg" );
    width: 100px;
    height: 100px;
}

.rotate {
    width: 100%;
    height: 100%;
    -webkit-animation: circle 10s infinite linear;
}
.counterrotate {
    width: 50%;
    height: 50%;
    -webkit-animation: ccircle 10s infinite linear;    
}

@-webkit-keyframes circle {
    from {-webkit-transform: rotateZ(0deg)}
    to {-webkit-transform: rotateZ(360deg)}
}

@-webkit-keyframes ccircle {
    from {-webkit-transform: rotateZ(360deg)}
    to {-webkit-transform: rotateZ(0deg)}
}
<div class="earth">
  <div class="rotate">
    <div class="counterrotate">
      <div class="moon"></div>
    </div>
  </div>
</div>

【讨论】:

  • 不客气(:我还找到了 Stéphanie Walter 的地球、月亮和太阳的复杂版本。如果您有兴趣,请查看以下链接:codepen.io/stephaniewalter/pen/vYGmrLR
  • 我只是做了一些修改。我删除了所有无用的代码,现在它干净了;在地球上添加深色背景和蓝色光芒作为大气。
【解决方案2】:

您可以尝试在定义其大小/形状的 CSS 规则中将每个图像添加为 background-image

【讨论】:

  • 谢谢。我试过了,但我可能遗漏了一些东西..你能看看我刚刚做的编辑吗?
【解决方案3】:

为您的元素添加背景图片。打开您的检查器并单击元素,它将突出显示元素边框,确定哪个元素是什么,然后在您的 CSS 中,将background:url(link to the image) 添加到您希望在其上放置图像的选择器/元素。

.outCircle {
  width: 200px;
  height: 200px;
  background-color: blue;
  left: 270px;
  position: absolute;
  top: 50px;
  -moz-border-radius: 100px;
  -webkit-border-radius: 100px;
  border-radius: 100px;
}

.rotate {
  width: 100%;
  height: 100%;
  -webkit-animation: circle 10s infinite linear;
  background: url(https://cdn.pixabay.com/photo/2017/05/12/22/48/mouse-2308339__180.jpg) no-repeat;
  border-radius: 50%;
}

.counterrotate {
  width: 50px;
  height: 50px;
  -webkit-animation: circle 10s infinite linear;
}

.inner {
  width: 100px;
  height: 100px;
  background: blue;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 100px;
  position: absolute;
  left: 0px;
  top: 0px;
  display: block;
  background: center url(https://as1.ftcdn.net/jpg/00/31/01/02/220_F_31010244_P6FGF9nfBY1oaGFndhdHhUUIfjHqMoib.jpg) no-repeat white;
  background-size: 90%;
}

@-webkit-keyframes circle {
  from {
    -webkit-transform: rotateZ(0deg)
  }
  to {
    -webkit-transform: rotateZ(360deg)
  }
}

@-webkit-keyframes ccircle {
  from {
    -webkit-transform: rotateZ(360deg)
  }
  to {
    -webkit-transform: rotateZ(0deg)
  }
}
<div class="outCircle">
  <div class="rotate">
    <div class="counterrotate">
      <div class="inner">Hello</div>
    </div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 2022-12-06
    • 1970-01-01
    相关资源
    最近更新 更多