简体   繁体   English

纯CSS3(3,2,1)倒计时

[英]Pure CSS3 (3, 2, 1) Countdown

I'm trying to create a 3 second countdown which automatically starts and then loops. 我正在尝试创建一个3秒的倒计时,该倒计时会自动开始然后循环。 Literally just saying '3' then replaces it with '2' and then finally '1'. 从字面上说“ 3”,然后将其替换为“ 2”,最后是“ 1”。

So far the code I'm trying is this: 到目前为止,我正在尝试的代码是这样的:

*[class].countdown:before {
    content: '3' !important;
    -webkit-animation:countdowns 3s ease-in-out infinite;
    -moz-animation:countdowns 3s ease-in-out infinite;
    -ms-animation:countdowns 3s ease-in-out infinite;
    -o-animation:countdowns 3s ease-in-out infinite;
    animation:countdowns 3s ease-in-out infinite;
}

@-webkit-keyframes countdowns {
    33% {content: '3' !important;}
    66% {content: '2' !important;}
    99% {content: '1' !important;}
}

Then I simply have this as the HTML: 然后,我将其作为HTML:

<span class="countdown"></span>

However this just displays '3' and nothing more. 但是,这仅显示“ 3”,仅此而已。 Anybody got any suggestions? 有人有任何建议吗? Thank you! 谢谢!

The content property cannot be animated so it gets ignored. content属性无法设置动画,因此将被忽略。

The @keyframes documentation states: @keyframes文档指出:

If you include properties that can't be animated in your keyframe rules, they get ignored, but the supported properties will still be animated. 如果您在关键帧规则中包含无法设置动画的属性,则会将其忽略,但支持的属性仍将被设置动画。


You can however do it in another way, if are allowed to use fixed-width fonts. 但是,如果允许使用固定宽度的字体,则可以用另一种方式进行。

.countdown:before {
    content: '321';

    font-family:monospace;
    display:inline-block;
    width:1ch;
    overflow:hidden;

    animation:countdowns 3s steps(3) infinite;
}

@keyframes countdowns {
    0% {text-indent:0}
    100% {text-indent:-3ch;}
}

Demo at http://jsfiddle.net/gaby/xtcvc4sw/ 演示位于http://jsfiddle.net/gaby/xtcvc4sw/

The content property isn't animate-able via CSS. content属性无法通过CSS设置动画。 Here's a good list that covers what is and what isn't animatable. 这是一个很好的清单 ,涵盖了什么是动画以及什么不是动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM