如何只使用CSS创建这个加载动画?

huangapple go评论46阅读模式
英文:

How can I create this loading animation using only CSS?

问题

以下是您提供的HTML和CSS代码的翻译部分:

我正在尝试使用HTML和CSS创建此加载动画,仅使用CSS进行动画处理,但我无法掌握时序。

[![输入图像描述][1]][1]

有什么提示可以帮助我使其工作?

<div style="display: flex; flex-direction: row; gap: 50px;">
  <div style="height: 100px; width: 100px; background-color: #ccc;">
    <div class="square"></div>
  </div>

  <div style="height: 100px; width: 100px; border: 1px solid white; position: relative;">
    <div id="squareToReveal-1" class="squareToReveal">1</div>
    <div id="squareToReveal-2" class="squareToReveal">2</div>
    <div id="squareToReveal-3" class="squareToReveal">3</div>
    <div id="squareToReveal-4" class="squareToReveal">4</div>
  </div>
</div>
body {
  background-color: #1E2226;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.square {
  width: 50px;
  height: 50px;
  background-color: white;
  
  position: relative;
  animation-name: moveSquare;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}

@keyframes moveSquare {
  0%    {top: 0; left: 0}
  25%   {top: 0; left: 50%;}
  50%   {top: 50%; left: 50%;}
  75%   {top: 50%; left: 0px;}
  100%  {top: 0px; left: 0px;}
}

.squareToReveal {
  width: 50px;
  height: 50px;
  background-color: #ccc;
  opacity: 0;
  
  position: absolute;
  animation-name: revealSquare;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}

#squareToReveal-1 {
  top: 0;
  left: 0;
  animation-delay: 0;
}

#squareToReveal-2 {
  top: 0;
  left: 50%;
  animation-delay: 0.5s;
}

#squareToReveal-3 {
  top: 50%;
  left: 50%;
  animation-delay: 1s;
}

#squareToReveal-4 {
  top: 50%;
  left: 0;
  animation-delay: 1.5s;
}

@keyframes revealSquare {
  0%    {opacity: 0;}
  25%   {opacity: 1;}
  75%   {opacity: 1;}
  100%   {opacity: 0;}
}

<details>
<summary>英文:</summary>
I am trying to create this loading animation in HTML and CSS using only CSS to animate it but I can&#39;t get the timing down.
[![enter image description here][1]][1]
Any tips on how I can get that to work?
```html
&lt;div style=&quot;display: flex; flex-direction: row; gap: 50px;&quot;&gt;
&lt;div style=&quot;height:100px;width:100px; background-color: #ccc&quot;&gt;
&lt;div class=&quot;square&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;height:100px;width:100px;border: 1px solid white; position: relative;&quot;&gt;
&lt;div id=&quot;squareToReveal-1&quot; class=&quot;squareToReveal&quot;&gt;1&lt;/div&gt;
&lt;div id=&quot;squareToReveal-2&quot; class=&quot;squareToReveal&quot;&gt;2&lt;/div&gt;
&lt;div id=&quot;squareToReveal-3&quot; class=&quot;squareToReveal&quot;&gt;3&lt;/div&gt;
&lt;div id=&quot;squareToReveal-4&quot; class=&quot;squareToReveal&quot;&gt;4&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
body {
  background-color: #1E2226;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.square {
  width: 50px;
  height: 50px;
  background-color: white;
  
  position: relative;
  animation-name: moveSquare;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}

@keyframes moveSquare {
  0%    {top: 0; left: 0}
  25%   {top: 0; left: 50%;}
  50%   {top: 50%; left: 50%;}
  75%   {top: 50%; left: 0px;}
  100%  {top: 0px; left: 0px;}
}

.squareToReveal {
  width: 50px;
  height: 50px;
  background-color: #ccc;
  opacity: 0;
  
  position: absolute;
  animation-name: revealSquare;
  animation-duration: 2s;
  animation-iteration-count: infinite;
}

#squareToReveal-1 {
  top: 0;
  left: 0;
  animation-delay: 0;
}

#squareToReveal-2 {
  top: 0;
  left: 50%;
  animation-delay: 0.5s;
}

#squareToReveal-3 {
  top: 50%;
  left: 50%;
  animation-delay: 1s;
}

#squareToReveal-4 {
  top: 50%;
  left: 0;
  animation-delay: 1.5s;
}

@keyframes revealSquare {
  0%    {opacity: 0;}
  25%   {opacity: 1;}
  75%   {opacity: 1;}
  100%   {opacity: 0;}
}

答案1

得分: 1

你可以应用 animation-timing-function: steps(1); 并将“tiles”动画分解为所需的帧。实际上,我们有8帧(抱歉照片质量不好):

如何只使用CSS创建这个加载动画?

body {
  background-color: #1E2226;
  display: grid;
  place-items: center;
  height: 100vh;
  margin: 0;
}

.loading {
  --size: 100px;
  --duration: 2s;
  position: relative;
  width: var(--size);
  height: var(--size);
  display: flex;
}

.loading:before {
  content: '';
  position: absolute;
  background-color: gray;
  inset: 0;
  animation: tile calc( var(--duration) * 2 ) steps(1) infinite;
}

.loading:after {
  content: '';
  width: calc(var(--size) / 2);
  height: calc(var(--size) / 2);
  background-color: #fff;
  animation: move var(--duration) linear infinite;
}

@keyframes move {
  25% {
    transform: translate(100%, 0);
  }
  50% {
    transform: translate(100%, 100%);
  }
  75% {
    transform: translate(0, 100%);
  }
}

@keyframes tile {
  0% {
    clip-path: polygon(0 0, 50% 0, 50% 50%, 0 50%);
  }
  12.5% {
    clip-path: polygon(0 0, 100% 0, 100% 50%, 0 50%);
  }
  25% {
    clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 100%, 50% 50%, 0 50%);
  }
  37.5% {
    clip-path: unset;
  }
  50% {
    clip-path: polygon(50% 0, 100% 0, 100% 100%, 0 100%, 0% 50%, 50% 50%);
  }
  62.5% {
    clip-path: polygon(0 50%, 100% 50%, 100% 100%, 0% 100%);
  }
  75% {
    clip-path: polygon(0 50%, 50% 50%, 50% 100%, 0% 100%);
  }
  87.5% {
    clip-path: polygon(0 0);
  }
}
<div class="loading"></div>
英文:

You can apply animation-timing-function: steps(1); and break the "tiles" animation into the required frames. In fact, we have 8 frames (sorry for the photo quality):

如何只使用CSS创建这个加载动画?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
background-color: #1E2226;
display: grid;
place-items: center;
height: 100vh;
margin: 0;
}
.loading {
--size: 100px;
--duration: 2s;
position: relative;
width: var(--size);
height: var(--size);
display: flex;
}
.loading:before {
content: &#39;&#39;;
position: absolute;
background-color: gray;
inset: 0;
animation: tile calc( var(--duration) * 2 ) steps(1) infinite;
}
.loading:after {
content: &#39;&#39;;
width: calc(var(--size) / 2);
height: calc(var(--size) / 2);
background-color: #fff;
animation: move var(--duration) linear infinite;
}
@keyframes move {
25% {
transform: translate(100%, 0);
}
50% {
transform: translate(100%, 100%);
}
75% {
transform: translate(0, 100%);
}
}
@keyframes tile {
0% {
clip-path: polygon(0 0, 50% 0, 50% 50%, 0 50%);
}
12.5% {
clip-path: polygon(0 0, 100% 0, 100% 50%, 0 50%);
}
25% {
clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 100%, 50% 50%, 0 50%);
}
37.5% {
clip-path: unset;
}
50% {
clip-path: polygon(50% 0, 100% 0, 100% 100%, 0 100%, 0% 50%, 50% 50%);
}
62.5% {
clip-path: polygon(0 50%, 100% 50%, 100% 100%, 0% 100%);
}
75% {
clip-path: polygon(0 50%, 50% 50%, 50% 100%, 0% 100%);
}
87.5% {
clip-path: polygon(0 0);
}
}

<!-- language: lang-html -->

&lt;div class=&quot;loading&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

使用关键帧来实现吗?

<div class="loading-txt"><p></p></div>

使用CSS中的::after

.loading-txt p::after {
    content: "0";
    animation: progressNum 2s infinite linear;
}

用于动画的关键帧

@keyframes progressNum {
    0% {
        content: "0";
    }
    50% {
        content: "50";
    }
    100% {
        content: "100";
    }
}

这可能只是一个修复来实现文本动画
英文:

can we use keyframes for that?

&lt;div class=&quot;loading-txt&quot;&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;

using ::after in CSS,

.loading-txt p::after{
content: &quot;0&quot;;
animation: progressNum 2s infinite linear;
}

for animation,

 @keyframes progressNum {
0% {
content: &quot;0&quot;;
}
50%{
content: &quot;50&quot;;
}
100% {
content: &quot;100&quot;;
}

}

this may be just a fix to animate text.

huangapple
  • 本文由 发表于 2023年6月6日 16:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定