Search code examples
jquerycssanimationcss-animationskeyframe

How can I make this CSS keyframe animation smooth when it restarts?


I have a CSS keyframe animation that is supposed to look like snow. It is set to last for 20 seconds and loop infinitely, which it does. The issue is that when the animation reaches the end after the set time and restarts, it is sort of a jerky/clunky restart.

Is there a way I can make it more smooth when the animation is looping so that it doesn't look like it is restarting but just a constant flow?

body {
    height: 100%;
    /*background-color: #333;*/
    background-color: #6b92b9;
    background-image: url('http://i.imgur.com/BiSmXaq.png'), url('http://i.imgur.com/XHuy0NJ.png'), url('http://i.imgur.com/okpRxJU.png');
    -webkit-animation: snow 20s linear infinite;
    -moz-animation: snow 20s linear infinite;
    -ms-animation: snow 20s linear infinite;
    animation: snow 20s linear infinite;
}

@-webkit-keyframes snow {
    0% {
        background-position: 0px 0px, 0px 0px, 0px 0px
    }
    50% {
        background-color: #b4cfe0
    }
    100% {
        background-position: 500px 1000px, 400px 400px, 300px 300px;
        background-color: #6b92b9;
    }
}

@-moz-keyframes snow {
    0% {
        background-position: 0px 0px, 0px 0px, 0px 0px
    }
    50% {
        background-color: #b4cfe0
    }
    100% {
        background-position: 500px 1000px, 400px 400px, 300px 300px;
        background-color: #6b92b9;
    }
}

@-ms-keyframes snow {
    0% {
        background-position: 0px 0px, 0px 0px, 0px 0px
    }
    50% {
        background-color: #b4cfe0
    }
    100% {
        background-position: 500px 1000px, 400px 400px, 300px 300px;
        background-color: #6b92b9;
    }
}

@keyframes snow {
    0% {
        background-position: 0px 0px, 0px 0px, 0px 0px
    }
    50% {
        background-color: #b4cfe0
    }
    100% {
        background-position: 500px 1000px, 400px 400px, 300px 300px;
        background-color: #6b92b9;
    }
}

Solution

  • background-position have to multiplication of original size. example, if your image size is 100x100 then background-position can be 100x100, 100x200, 200x100, 200x200, and so on

    body {
      height: 100%;
      /*background-color: #333;*/
      background-color: #6b92b9;
      background-image: url('http://i.imgur.com/BiSmXaq.png'), url('http://i.imgur.com/XHuy0NJ.png'), url('http://i.imgur.com/okpRxJU.png');
      animation: snow 5s linear infinite both;
    }
    @keyframes snow {
      0% {
        background-position: 0px 0px, 0px 0px, 0px 0px;
      }
      50% {
        background-color: #b4cfe0;
      }
      100% {
        background-position: 300px 300px, 400px 400px, 500px 500px;
        background-color: #6b92b9;
      }
    }