【问题标题】:Replicate the SVG stroke-dasharray using only HTML and SCSS (or CSS)仅使用 HTML 和 SCSS(或 CSS)复制 SVG stroke-dasharray
【发布时间】:2019-09-09 15:48:56
【问题描述】:

我正在尝试创建类似于此示例的经典圆形加载器 https://dribbble.com/shots/1918018-Circle-Loading-Animation

所以,我知道可以将 SVG 与 stroke-dasharray 一起使用,并且肯定会是最简单的方法。 顺便说一句,我在问是否有另一种方法可以只使用 SCSS(或 CSS)来做到这一点。

我的目标是复制这种加载器,而不向 HTML 文件添加任何 SVG。

谢谢!

【问题讨论】:

  • 如果您不想要任何额外的 html,也可以使用动画 svg 作为加载器的背景。
  • 这是stroke-dashoffset 不是笔划偏移。你还需要一个stroke-dasharray。您可以在 CSS 中使用它。我想知道你为什么不想使用这种技术。

标签: css svg sass frontend


【解决方案1】:

如果您可以接受最小的stroke-dashoffset 大约是圆周长的 25%,您可以按照以下方式进行。

基本上你有四个正方形的子元素。每个都有一个圆形边框,覆盖正方形的一侧(即圆周的 25%)。这些子元素都重叠。然后用稍微不同的动画开始时间来旋转每个孩子。

.lds-ring {
  display: inline-block;
  position: relative;
  width: 64px;
  height: 64px;
}
.lds-ring div {
  box-sizing: border-box;
  display: block;
  position: absolute;
  width: 51px;
  height: 51px;
  margin: 6px;
  border: 6px solid #fff;
  border-radius: 50%;
  animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
  border-color: #fff transparent transparent transparent;
}
.lds-ring div:nth-child(1) {
  animation-delay: -0.45s;
}
.lds-ring div:nth-child(2) {
  animation-delay: -0.3s;
}
.lds-ring div:nth-child(3) {
  animation-delay: -0.15s;
}
@keyframes lds-ring {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}


body {
  background-color: black;
}
<div class="lds-ring">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

来源:https://loading.io/css/

【讨论】:

  • 太棒了,我正在寻找类似的东西!谢谢!
猜你喜欢
  • 2021-08-08
  • 2018-05-12
  • 2019-08-18
  • 2016-03-13
  • 2019-04-03
  • 2021-03-26
  • 2017-08-01
  • 2013-12-23
  • 2014-04-02
相关资源
最近更新 更多