CSS动画增量计数器

时间:2017-11-06 15:21:09

标签: css css3 css-animations css-counter

我最近一直在玩CSS计数器,我想知道是否可以将它们放在CSS动画循环中并递增它们。我的代码很简单:

div {
    animation-name: anim;
    animation-duration: 0.5s;
    animation-iteration-count: 10;
    animation-fill-mode: forwards;
}

div:before {
    counter-increment: variable;
    content: counter(variable);
}

@keyframes anim {
    100% { counter-increment: variable; }
}
<div></div>

你可以看到数字上升,但随后又回到1.我认为animation-fill-mode: forwards会阻止这种情况发生,但我猜不会。

我做错了什么,或者CSS计数器无法做到这一点?

1 个答案:

答案 0 :(得分:-2)

使用简单的脚本代码可以轻松实现,试试这个

<div><span class="count">200</span></div>
<div><span class="count">177</span></div>

$('.count').each(function () {
    $(this).prop('Counter',0).animate({
       Counter: $(this).text()
    }, {
       duration: 4000,
       easing: 'swing',
       step: function (now) {
          $(this).text(Math.ceil(now));
       }
   });
});

Refer This

相关问题