首页 > 解决方案 > 用 css 动画点,使它们永远移动

问题描述

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

这是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>

标签: csscss-animations

解决方案


您可以使用简单的背景来执行此操作,这样可以轻松控制动画以及点的尺寸:

.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 percent values with background-position on a linear gradient


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

.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>


推荐阅读