是否可以在CSS(开环形状)中绘制部分圆形轮廓?

col*_*unn 23 css css3 css-animations css-shapes

我有兴趣在CSS中创建一个加载微调器,但为了做到这一点,我需要能够绘制一个像这样的开环形状:

在此输入图像描述

环会围绕圆周绘制自己.这在CSS中是否可以实现?

Dyl*_*ark 28

要创建一个逐渐绘制其外部路径的圆,请使用SVG.

SVG的stroke-dasharray属性会将任何路径转换为虚线,您可以通过将虚线大小设置为与路径本身一样长的方式来利用它.

然后使用CSS动画逐渐更改stroke-dashoffset以围绕圆周边移动短划线.

circle {
  fill: white;
  stroke: black;
  stroke-width: 2;
  stroke-dasharray: 250;
  stroke-dashoffset: 1000;
  animation: rotate 5s linear infinite;
}

@keyframes rotate {
  to {
    stroke-dashoffset: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" />
</svg>
Run Code Online (Sandbox Code Playgroud)


Rio*_*ams 15

静态图像

仅依赖于单个HTML元素和CSS类的简化示例可能如下所示:

.arc {
  /* Border size and color */
  border: 2px solid #000;
  /* Creates a circle */
  border-radius: 50%;
  /* Circle size */
  height: 100px;
  width: 100px;
  /* Use transparent borders to define opening (more transparent = larger opening) */
  border-top-color: transparent;
  border-left-color: transparent;
  /* Use transform to rotate to adjust where opening appears */
  transform: rotate(300deg)
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

.arc {
  border: 2px solid #000;
  border-radius: 50%;
  height: 100px;
  width: 100px;
  border-top-color: transparent;
  transform: rotate(300deg)
}
Run Code Online (Sandbox Code Playgroud)
<div class='arc'></div>
Run Code Online (Sandbox Code Playgroud)

旋转图像

您可以使用@keyframes以下基于CSS的动画将基本旋转应用于上一个静态示例:

.arc {
  /* Border size and color */
  border: 2px solid #000;
  /* Creates a circle */
  border-radius: 50%;
  /* Circle size */
  height: 100px;
  width: 100px;
  /* Use transparent borders to define opening (more transparent = larger opening) */
  border-top-color: transparent;
  /* Rotate indefinitely (longer time = slower rotation) */
  animation: rotate 2s infinite linear;
}

@keyframes rotate {
  0%    { transform: rotate(0deg);  }
  100%  { transform: rotate(360deg);  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

.arc {
  border: 2px solid #000;
  border-radius: 50%;
  height: 100px;
  width: 100px;
  border-top-color: transparent;
  animation: rotate 2s infinite linear;
}

@keyframes rotate {
  0%    { transform: rotate(0deg);  }
  100%  { transform: rotate(360deg);  }
}
Run Code Online (Sandbox Code Playgroud)
<div class='arc'></div>
Run Code Online (Sandbox Code Playgroud)

绘图(没有SVG)

我遇到的另一种方法,虽然不像以前的方法那样优雅,但似乎达到了你想要的效果.在涉及使用几个动画以及根据需要显示/隐藏圆圈的不同部分.

代码段包含一个演示它的示例.

在此输入图像描述

#container {
  position: absolute;
  width: 100px;
  height: 100px;
  animation: colors 1s infinite;
}
#halfclip {
  width: 50%;
  height: 100%;
  right: 0px;
  position: absolute;
  overflow: hidden;
  transform-origin: left center;
  animation: cliprotate 4s steps(2) infinite;
  -webkit-animation: cliprotate 4s steps(2) infinite;
}
.halfcircle {
  box-sizing: border-box;
  height: 100%;
  right: 0px;
  position: absolute;
  border: solid 2px transparent;
  border-top-color: #000;
  border-left-color: #000;
  border-radius: 50%;
}
#clipped {
  width: 200%;
  animation: rotate 2s linear infinite;
  -webkit-animation: rotate 2s linear infinite;
}
#fixed {
  width: 100%;
  transform: rotate(135deg);
  animation: showfixed 4s steps(2) infinite;
  -webkit-animation: showfixed 4s linear infinite;
}
@-webkit-keyframes cliprotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes cliprotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes rotate {
  0% {
    transform: rotate(-45deg);
  }
  100% {
    transform: rotate(135deg);
  }
}
@keyframes rotate {
  0% {
    transform: rotate(-45deg);
  }
  100% {
    transform: rotate(135deg);
  }
}
@-webkit-keyframes showfixed {
  0% {
    opacity: 0;
  }
  49.9% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 1;
  }
}
Run Code Online (Sandbox Code Playgroud)
<div id="container">
  <div id="halfclip">
    <div class="halfcircle" id="clipped">
    </div>
  </div>
  <div class="halfcircle" id="fixed">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

绘图(使用SVG)

利用SVG可能是解决此问题的最佳方法,因为它明确地设计用于处理浏览器中的绘图.如果SVG支持可用,我强烈推荐这种方法.

Dylan的回复详细说明了这种实现的样子.


and*_*eas 6

您可以只使用伪元素::after来创建开放部分,只需重叠圆形元素即可。优点是开口部分可以任意长(不限于3/4整圆)。

.circle {
  width: 100px;
  height: 100px;
  border: 2px solid;
  border-radius: 50%;
  margin: 30px;
  animation: rotate 1s infinite linear;
}
.circle::after {
  content: "";
  display: block;
  width: 80px;
  height: 80px;
  background: white;
  border-radius: 50%;
  margin: -30% 0 0 -30%;
}
@keyframes rotate {
  0%    { transform: rotate(0deg);  }
  100%  { transform: rotate(360deg);  }
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle"></div>
Run Code Online (Sandbox Code Playgroud)