带有css的动画点,使其永远移动

时间:2019-04-07 18:46:18

标签: css css-animations

如果不在跨度中添加更多点,我似乎无法使该动画永远移动。 我希望点的数量不依赖于“加载点”类,因为添加更多的点会增加持续时间,但很痛苦。可能只有一个点,但会永久设置它的动画。我喜欢改变速度和方向的能力。

这里是CodePen

* {
  box-sizing: border-box;
}

body {
  padding: 50px;
  background: white;
}

.container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px 20px 0px 20px;
}

.loading-container {
  overflow: hidden;
  float: left;
  width: 200px;
}

.loading-dots {
  display: inline-block;
  animation-name: loading-dots;
  animation-duration: 5s;
  animation-timing-function: linear;
  font-size: 50px;
  position: relative;
  top: -27px;
  color: rgba(blue, 1);
  font-family: sans-serif;
  white-space: no-wrap;
}

.loading-title {
  overflow: display;
  position: relative;
  font-size: 30px;
  top: 10px;
  margin-right: 10px;
  font-family: monospace;
  color: rgba(white, 1);
  float: left;
}

@keyframes loading-dots {
  0% {
    transform: translateX(-600px);
  }
  100% {
    transform: translateX(0px);
  }
}
<div class="container">
  <span class="loading-title"></span>
  <div class="loading-container">
    <span class="loading-dots">.................
      </span>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

您可以使用简单的背景进行操作,在该背景下可以轻松控制动画以及点的尺寸:

.dots {
  height:5px; /*to control the overall height*/
  width:200px; /*to control the overall width*/
  margin:50px;
  background-image:
    repeating-linear-gradient(to right,
     transparent,transparent 5px, /*5px of transparent*/
     blue 5px,blue 10px); /*then 5px of blue */
  background-size:200% 100%;
  animation:change 3s linear infinite;
}

@keyframes change{
  to {
    background-position:right;
  }
}
<div class="dots"></div>

要更改方向,只需更改位置即可。

.dots {
  height:5px;
  width:200px;
  margin:50px;
  background-image:
    repeating-linear-gradient(to right,
     transparent,transparent 5px,
     blue 5px,blue 10px);
  background-size:200% 100%;
  background-position:right;
  animation:change 3s linear infinite;
}

@keyframes change{
  to {
    background-position:left;
  }
}
<div class="dots"></div>

您可以检查有关使用的不同值的更多详细信息:Using percentage values with background-position on a linear gradient


在变换上使用动画的另一个想法:

.dots {
  height:5px;
  width:200px;
  margin:50px;
  position:relative;
  overflow:hidden;
}
.dots::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:-100%;
  bottom:0;
  background-image:
    repeating-linear-gradient(to right,
     transparent,transparent 5px,
     blue 5px,blue 10px);
  animation:change 3s linear infinite;

}

@keyframes change{
  to {
    transform:translateX(-50%);
  }
}
<div class="dots"></div>

更改方向:

.dots {
  height:5px;
  width:200px;
  margin:50px;
  position:relative;
  overflow:hidden;
}
.dots::before {
  content:"";
  position:absolute;
  top:0;
  left:-100%;
  right:0;
  bottom:0;
  background-image:
    repeating-linear-gradient(to right,
     transparent,transparent 5px,
     blue 5px,blue 10px);
  animation:change 3s linear infinite;

}

@keyframes change{
  to {
    transform:translateX(50%);
  }
}
<div class="dots"></div>

答案 1 :(得分:0)

减少负像素边距。 -600px对于16个点来说非常过分。

@keyframes loading-dots {
  0% {
    transform: translateX(-50px);
  }
  100% {
    transform: translateX(0px);
  }
}