无论如何使用CSS动画制作省略号动画?

Rey*_*Rey 80 css css3 css-animations

我正在尝试使用省略号动画,并且想知道CSS动画是否可行......

所以它可能就像

Loading...
Loading..
Loading.
Loading...
Loading..
Run Code Online (Sandbox Code Playgroud)

基本上就这样继续下去.有任何想法吗?

编辑:像这样:http://playground.magicrising.de/demo/ellipsis.html

the*_*eks 75

如何稍微修改一下@ xec的答案:http://codepen.io/thetallweeks/pen/yybGra

HTML

添加到元素的单个类:

<div class="loading">Loading</div>
Run Code Online (Sandbox Code Playgroud)

CSS

使用的动画steps.请参阅MDN文档

.loading:after {
  overflow: hidden;
  display: inline-block;
  vertical-align: bottom;
  -webkit-animation: ellipsis steps(4,end) 900ms infinite;      
  animation: ellipsis steps(4,end) 900ms infinite;
  content: "\2026"; /* ascii code for the ellipsis character */
  width: 0px;
}

@keyframes ellipsis {
  to {
    width: 20px;    
  }
}

@-webkit-keyframes ellipsis {
  to {
    width: 20px;    
  }
}
Run Code Online (Sandbox Code Playgroud)

@ xec的答案对点有更多的滑入效果,而这可以让点立即出现.

  • 如果你在对中或右对齐的文本上使用它,我建议添加一个'20px`的初始`margin-right`(或填充?),如果你不想要你的文本,可以将它设置为'0px`在动画期间转移. (8认同)
  • 我的意思是你三年后确实回答了这个问题,但这可能更好。 (3认同)
  • @xckpd7 是的,但我今天在谷歌上搜索了这个并找到了这个答案。SO 不仅适用于 OP,它还是所有人的资源! (3认同)

Chr*_*der 53

您可以尝试使用animation-delay property每个省略号字符的时间和时间.在这种情况下,我将每个省略号字符放在一个<span class>所以我可以单独为它们设置动画.

我做了一个演示,这不是完美的,但它至少显示了我的意思:)

我的例子中的代码:

HTML

Loading<span class="one">.</span><span class="two">.</span><span class="three">.</span>?
Run Code Online (Sandbox Code Playgroud)

CSS

.one {
    opacity: 0;
    -webkit-animation: dot 1.3s infinite;
    -webkit-animation-delay: 0.0s;
    animation: dot 1.3s infinite;
    animation-delay: 0.0s;
}

.two {
    opacity: 0;
    -webkit-animation: dot 1.3s infinite;
    -webkit-animation-delay: 0.2s;
    animation: dot 1.3s infinite;
    animation-delay: 0.2s;
}

.three {
    opacity: 0;
    -webkit-animation: dot 1.3s infinite;
    -webkit-animation-delay: 0.3s;
    animation: dot 1.3s infinite;
    animation-delay: 0.3s;
}

@-webkit-keyframes dot {
    0% {
        opacity: 0;
    }
    50% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

@keyframes dot {
    0% {
        opacity: 0;
    }
    50% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对不起@Christofer - 忘了保存我的更新小提琴.这里又是:http://jsfiddle.net/toddwprice/cRLMw/8/此外,这里有一篇我刚读过的文章,里面有一些有趣的CSS动画:http://tympanus.net/Tutorials/LoadingAnimations/index4.html (2认同)
  • 我稍微调整了 HTML 和 CSS 以使用 &lt;i&gt; 标签... http://jsfiddle.net/DkcD4/77/ (2认同)

Cod*_*uer 29

即使是更简单的解决方案,也能很好地运行!

<style>
    .loading:after {
      display: inline-block;
      animation: dotty steps(1,end) 1s infinite;
      content: '';
    }

    @keyframes dotty {
        0%   { content: ''; }
        25%  { content: '.'; }
        50%  { content: '..'; }
        75%  { content: '...'; }
        100% { content: ''; }
    }
</style>
<div class="loading">Loading</div>
Run Code Online (Sandbox Code Playgroud)

刚用动画编辑内容而不是隐藏一些点......

在这里演示:https://jsfiddle.net/f6vhway2/1/


编辑: 感谢@BradCollins指出这content不是一个可动画的属性.

https://www.w3.org/TR/css3-transitions/#animatable-css

所以这是一个仅限WebKit/Blink/Electron的解决方案.(但它也适用于当前的FF版本)

  • +1 用于动画“内容”。为了获得均匀的动画节奏,您应该使用“steps(1)”并定义一个额外的关键帧。步骤函数规定了关键帧之间的步骤数,并且由于我们指定了每个帧,所以我们只需要它们之间有一个步骤。http://codepen.io/anon/pen/VmEdXj (2认同)

xec*_*xec 16

简短的回答是"不是真的".但是,您可以使用动画宽度和溢出隐藏,并可能获得"足够接近"的效果.(下面的代码仅针对firefox定制,根据需要添加供应商前缀).

HTML

<div class="loading">Loading</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.loading:after {
    overflow: hidden;
    display: inline-block;
    vertical-align: bottom;
    -moz-animation: ellipsis 2s infinite;
    content: "\2026"; /* ascii code for the ellipsis character */
}
@-moz-keyframes ellipsis {
    from {
        width: 2px;
    }
    to {
        width: 15px;
    }
}
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/MDzsR/1/

编辑

看来chrome有动画伪元素的问题.一个简单的解决方法是将省略号包装在自己的元素中.查看http://jsfiddle.net/MDzsR/4/


小智 6

添加较晚,但我找到了一种支持居中文本的方法。

<element>:after {
    content: '\00a0\00a0\00a0';
    animation: progress-ellipsis 5s infinite;
}

@keyframes progress-ellipsis {
    0% {
        content: '\00a0\00a0\00a0';
    }
    30% {
        content: '.\00a0\00a0';
    }
    60% {
        content: '..\00a0';
    }
    90% {
        content: '...';
    }
}
Run Code Online (Sandbox Code Playgroud)


Jak*_*b E 5

您可以制作动画(如果不需要 IE 支持则clip更好)clip-path

div {
  display: inline-block;
  font-size: 1.4rem;
}

div:after {
  position: absolute;
  margin-left: .1rem;
  content: ' ...';
  animation: loading steps(4) 2s infinite;
  clip: rect(auto, 0px, auto, auto);
}

@keyframes loading {
  to {
    clip: rect(auto, 20px, auto, auto);
  }
}
Run Code Online (Sandbox Code Playgroud)
<div>Loading</div>
Run Code Online (Sandbox Code Playgroud)