Stop infinite CSS3 animation and smoothly revert to initial state

后端 未结 1 1187
死守一世寂寞
死守一世寂寞 2021-01-06 01:21

Having some trouble building a CSS3 loader using keyframe animations.

The loader consists of 4 boxes that animate going up and down. The issue I\'m having is that wh

相关标签:
1条回答
  • 2021-01-06 01:42

    Found a pretty simple workaround. Still not pure CSS, it involves a bit of JS, but it works well.

    Updated fiddle: https://jsfiddle.net/cazacuvlad/qjmhm4ma/2/

    What I did was to move the loader-active class to each span (instead of the wrapper), listen to the animationiteration event on each span and stop the animation then.

    $('.loader-wrapper span').on('animationiteration webkitAnimationIteration', function () {
      var $this = $(this);
    
      $this.removeClass('loader-active');
    
      $this.off();
    });
    

    This basically stops the animation at the very end of an iteration cycle.

    Updated LESS

    .loader-wrapper {
      span {
        &.loader-active {
          .animation-name(loader);
          .animation-duration(1200ms);
          .animation-timing-function(ease-in-out);
          .animation-play-state(running);
          .animation-iteration-count(infinite);
    
          &:nth-child(1) {
          }
          &:nth-child(2) {
            .animation-delay(300ms);
          }
          &:nth-child(3) {
            .animation-delay(600ms);
          }
          &:nth-child(4) {
            .animation-delay(900ms);
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题