我正在尝试让省略号动画化,想知道是否可以使用CSS动画实现...
就像这样
Loading...
Loading..
Loading.
Loading...
Loading..
然后就像那样继续进行。有任何想法吗?
我正在尝试让省略号动画化,想知道是否可以使用CSS动画实现...
就像这样
Loading...
Loading..
Loading.
Loading...
Loading..
然后就像那样继续进行。有任何想法吗?
这里是稍作修改的版本,基于@xec的答案:http://codepen.io/thetallweeks/pen/yybGra
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: 40px;
}
}
@-webkit-keyframes ellipsis {
to {
width: 40px;
}
}
<h1 class="loading">Loading</h1>
@xec的答案更像是滑入效果,而这个效果可以让圆点立即出现。
margin-right
(或padding?)为20px
,并在动画期间将其动画化为0px
,以避免文本在动画期间移位。 - Kimball1em
代替20px
可能效果更好。 - Jeevan Takhar甚至更简单的解决方案也能很好地工作!
<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>
将内容改为以动画方式显示而不是隐藏一些点...
演示在此处:https://jsfiddle.net/f6vhway2/1/
编辑:
感谢@BradCollins指出content
是不可动画化的属性。
目前(2021年),这适用于Chrome/WebKit/Blink/Electron和Firefox以及新版本的Edge。
content
添加动画效果。为了获得均匀的动画节奏,您应该使用 steps(1)
并定义一个额外的关键帧。步骤函数决定了关键帧之间的步数,由于我们正在指定每一帧,因此我们只需要在它们之间进行单个步骤。http://codepen.io/anon/pen/VmEdXj - Jeff Cameraanimation-delay 属性
并计时每个省略号字符。在这种情况下,我将每个省略号字符都放在一个 <span class>
中,以便可以单独进行动画处理。
我制作了一个演示,虽然不是完美的,但至少展示了我的意思 :)
我的示例代码:
HTML
Loading<span class="one">.</span><span class="two">.</span><span class="three">.</span>
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;
}
}
简短的回答是“不太可能”。但是,你可以尝试使用动画宽度和溢出隐藏进行实验,或许可以获得足够接近的效果。(下面的代码仅适用于火狐浏览器,请根据需要添加供应商前缀)。
html
<div class="loading">Loading</div>
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;
}
}
演示:http://jsfiddle.net/MDzsR/1/
编辑
Chrome似乎存在与伪元素动画的问题。一个简单的解决方法是将省略号包裹在自己的元素中。请查看http://jsfiddle.net/MDzsR/4/
<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: '...';
}
}
如果您不需要支持IE浏览器,可以使用clip-path
代替clip
进行动画效果。
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);
}
}
<div>Loading</div>
实际上,有一种使用纯CSS的方法来做到这一点。
我从CSS Tricks获取了这个例子,但也使其在Internet Explorer中得到支持(我已在10+中测试过)。
点击查看演示:http://jsfiddle.net/Roobyx/AT6v6/2/
HTML:
<h4 id="searching-ellipsis"> Searching
<span>.</span>
<span>.</span>
<span>.</span>
</h4>
CSS:
@-webkit-keyframes opacity {
0% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
100% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
}
}
@-moz-keyframes opacity {
0% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
100% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
}
}
@-webkit-keyframes opacity {
0% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
100% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
}
}
@-moz-keyframes opacity {
0% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
100% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
}
}
@-o-keyframes opacity {
0% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
100% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
}
}
@keyframes opacity {
0% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}
100% {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
}
}
#searching-ellipsis span {
-webkit-animation-name: opacity;
-webkit-animation-duration: 1s;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: opacity;
-moz-animation-duration: 1s;
-moz-animation-iteration-count: infinite;
-ms-animation-name: opacity;
-ms-animation-duration: 1s;
-ms-animation-iteration-count: infinite;
}
#searching-ellipsis span:nth-child(2) {
-webkit-animation-delay: 100ms;
-moz-animation-delay: 100ms;
-ms-animation-delay: 100ms;
-o-animation-delay: 100ms;
animation-delay: 100ms;
}
#searching-ellipsis span:nth-child(3) {
-webkit-animation-delay: 300ms;
-moz-animation-delay: 300ms;
-ms-animation-delay: 300ms;
-o-animation-delay: 300ms;
animation-delay: 300ms;
}
clip-path
最清晰,具有以下好处:
不使用width
,因此:
.loading-ellipsis:after {
overflow: hidden;
display: inline-block;
vertical-align: bottom;
animation: ellipsis-animation steps(1,end) 2s infinite;
content: "\2026"; /* ascii code for the ellipsis character */
/* Enable this to see what is going on: */
/* background-color: red; */
}
@keyframes ellipsis-animation {
0% { clip-path: inset(0 100% 0 0); }
25% { clip-path: inset(0 66.6% 0 0); }
50% { clip-path: inset(0 33.3% 0 0); }
75% { clip-path: inset(0 0 0 0); }
}
<span class="loading-ellipsis">Loading</span> More text behind it that does not move
steps(1,end)
。这是有效的,因为我将动画结束时设置为75%
,这样最后一步就显示了省略号(第三个点)的完全展开。100% { clip-path: inset(0 0 0 0); }
,不需要写出来。)我完全按照{{link1:@CodeBrauer}所做的那样,但因为我的文本是text-align:center
(这也适用于right
),而且我不想让文本每次添加句号时都移动,所以我添加了“标点空格”:
<style>
.loading::after {
display: inline-block;
animation: dotty steps(1,end) 1s infinite;
content: '';
}
@keyframes dotty {
0% { content: '\2008\2008\2008'; }
25% { content: '.\2008\2008'; }
50% { content: '..\2008'; }
75% { content: '...'; }
100% { content: '\2008\2008\2008'; }
}
</style>
<div class="loading">Loading</div>
.dot1 {
animation: visibility 3s linear infinite;
}
@keyframes visibility {
0% {
opacity: 1;
}
65% {
opacity: 1;
}
66% {
opacity: 0;
}
100% {
opacity: 0;
}
}
.dot2 {
animation: visibility2 3s linear infinite;
}
@keyframes visibility2 {
0% {
opacity: 0;
}
21% {
opacity: 0;
}
22% {
opacity: 1;
}
65% {
opacity: 1;
}
66% {
opacity: 0;
}
100% {
opacity: 0;
}
}
.dot3 {
animation: visibility3 3s linear infinite;
}
@keyframes visibility3 {
0% {
opacity: 0;
}
43% {
opacity: 0;
}
44% {
opacity: 1;
}
65% {
opacity: 1;
}
66% {
opacity: 0;
}
100% {
opacity: 0;
}
}
HTML
Loading <span class="dot dot1">.</span><span class="dot dot2">.</span><span class="dot dot3">.</span>