Make a pause during infinite CSS3 animation

前端 未结 3 1654
星月不相逢
星月不相逢 2021-01-11 09:40

I try to make an animation that run every 3 seconds without JavaScript. My animation\'s duration is 1 second.

I\'m only able to wait 3seconds the fi

相关标签:
3条回答
  • 2021-01-11 10:18

    I am not sure exactly when it was released, and it's not the most cross-browser approach (does not cover older browsers like I.E. 9) but you could use the animationPlayState style prop. Theres some documentation on this found here if you want to check it out.

    animationPlayState=false
    webkitAnimationPlayState=false
    
    function pause() {
        var animationElement = document.getElementById('animatedItem');
        animationElement.style.animationPlayState='paused';
        animationElement.style.webkitAnimationPlayState='paused';
    }
    

    As you can see this put's the items animation into a "paused"state, to put it back where it left the animation off at, you can set it to the "running" state that this prop accepts.

    Here is a quick example I found when browsing Google.

    0 讨论(0)
  • 2021-01-11 10:21

    I was able to do this, as @Josh mentioned, by extending the animation. For example, if you want your animation to last 0.5 seconds with a 3 second pause, you make the entire animation 3.5 seconds, and then use percentages to extend it. (0.5 seconds is about 14% of 3.5 seconds.)

    In the code below, I created a DIV with transparent gradient that is the same width as the parent, and then animated it from left to right to give a shimmer effect.

    .shimmer {
        height: 100%;
        width: 100%;
        position: absolute;
        top: 0px;
    
        background-image: -moz-linear-gradient(160deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0) 25%, rgba(255,255,255,0.85) 60%, rgba(255,255,255,0) 100%); /* FF3.6+ */
        background-image: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(255,255,255,0)), color-stop(25%,rgba(255,255,255,0)), color-stop(60%,rgba(255,255,255,0.85)), color-stop(100%,rgba(255,255,255,0))); /* Chrome,Safari4+ */
        background-image: -webkit-linear-gradient(160deg, rgba(255,255,255,0) 0%,rgba(255,255,255,0) 25%,rgba(255,255,255,0.85) 60%,rgba(255,255,255,0) 100%); /* Chrome10+,Safari5.1+ */
        background-image: -o-linear-gradient(160deg, rgba(255,255,255,0) 0%,rgba(255,255,255,0) 25%,rgba(255,255,255,0.85) 60%,rgba(255,255,255,0) 100%); /* Opera11.10+ */
        background-image: -ms-linear-gradient(160deg, rgba(255,255,255,0) 0%,rgba(255,255,255,0) 25%,rgba(255,255,255,0.85) 60%,rgba(255,255,255,0) 100%); /* IE10+ */
        background-image: linear-gradient(160deg, rgba(255,255,255,0) 0%,rgba(255,255,255,0) 25%,rgba(255,255,255,0.85) 60%,rgba(255,255,255,0) 100%); /* W3C */
        background-repeat: repeat-y;
        background-size: 30% 100%;
        left: -100%;
    
        z-index: 101;
    
        animation-name: shine;
        animation-duration: 3.5s;
        animation-iteration-count: infinite;
        animation-timing-function: ease-in-out;
    }
    
    @keyframes shine {
        0% { left: -100%; }
        14% { left: 100%; }
        100% { left: 100%; }
    }
    

    From 14% to 100%, the DIV doesn't move, but the animation continues for additional time, giving the effect of a pause.

    0 讨论(0)
  • 2021-01-11 10:26

    It seems like the only way to achieve this is to extend the animation so that it lasts 4s instead of 1s. Then you could delay the animation by animating from 75% to 100% (rather than 0% to 100%).

    In doing so, there is essentially an artificial delay in the animation itself. You just have to do the math to figure out how long this delay is in correlation with the total length of the animation itself.

    Updated Example

    .face.back {
          display: block;
          transform: rotateY(180deg);
    }
    
    .face.back {
        -webkit-animation: BackRotate 4s linear infinite;
        animation: BackRotate 4s linear infinite;
    }
    
    .face.front {
        -webkit-animation: Rotate 4s linear infinite;
        animation: Rotate 4s linear infinite;
    }
    
    
    @-webkit-keyframes Rotate {
        75% {-webkit-transform:rotateY(0deg);}
        100% {-webkit-transform:rotateY(360deg);}
    }
    @-webkit-keyframes BackRotate {
        75% {-webkit-transform:rotateY(180deg);}
        100% {-webkit-transform:rotateY(540deg);}
    } 
    @keyframes Rotate {
        75% {-webkit-transform:rotateY(0deg);}
        100% {-webkit-transform:rotateY(360deg);}
    }
    @keyframes BackRotate {
        75% {-webkit-transform:rotateY(180deg);}
        100% {-webkit-transform:rotateY(540deg);}
    }
    
    0 讨论(0)
提交回复
热议问题