CSS 3スプライトアニメーション

スプライトアニメーションは、原始的であるにもかかわらず、四半世紀以上にわたってコンピュータグラフィックスやゲームで成功し、使用されてきたものの1つです。 3次元のゲームでも、スプライトがあります。たとえば、爆発の看板です。 多くのブラウザやフラッシュゲームでは、スプライトアニメーションを使用しています。これは非常にシンプルで高性能を必要としないためです-フレームを切り替えるだけです。 また、CSS 3でのアニメーションの出現により、JavaScriptなしでページでスプライトを使用できるようになりました。



今すぐ以下の方法がWebkitブラウザーでのみ機能することを予約します。

それでは、3つのフレームの単純なスプライトを見てみましょう。



必要なのは、divに背景を配置し、時間とともに背景の位置を変更することだけです。 すべてがシンプルなようです:

Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  1. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  2. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  3. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  4. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  5. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  6. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  7. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  8. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  9. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  10. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  11. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  12. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  13. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  14. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  15. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  16. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  17. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  18. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  19. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  20. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  21. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  22. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  23. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  24. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  25. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  26. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  27. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  28. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  29. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  30. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  31. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  32. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  33. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  34. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }



  35. Copy Source | Copy HTML .sprite { position : absolute ; left : 50% ; /* */ top : 33% ; width : 32px ; /* */ height : 32px ; margin : -16px 0 0 -16px ; /* :-) */ background : url ( sprite.png ) no-repeat 0 0 ; /* */ -webkit-animation-name: sprite ; /* */ -webkit-animation-duration: .3s; /* 300 */ -webkit-animation-iteration-count: infinite ; /* */ -webkit-animation-timing-function: linear ; /* */ } /* sprite */ @-webkit-keyframes sprite { /* , 0 */ 0% { background-position : -0px 0 ; } 33% { background-position : -32px 0 ; } 66% { background-position : -64px 0 ; } 100% { background-position : -0px 0 ; } }





しかし、悲しいかな! その結果、 ポルノは1つしかありません。 ぎくしゃくしたフレームの長さではなく、画像がスムーズに移動します。 長い間隔を空けてフレームをすばやく切り替えることで、状況を改善してみましょう。

Copy Source | Copy HTML



  1. @ -webkit-keyframesスプライト
  2. {
  3. 0%
  4. {
  5. バックグラウンド位置-0px 0 ;
  6. }
  7. 33.332%
  8. {
  9. バックグラウンド位置-0px 0 ;
  10. }
  11. 33.334%
  12. {
  13. background-position-32px 0 ;
  14. }
  15. 66.665%
  16. {
  17. background-position-32px 0 ;
  18. }
  19. 66.667%
  20. {
  21. バックグラウンド位置-64px 0 ;
  22. }
  23. 99.999%
  24. {
  25. バックグラウンド位置-64px 0 ;
  26. }
  27. 100%
  28. {
  29. バックグラウンド位置-0px 0 ;
  30. }
  31. }


コードは非常に膨大であることが判明しましたが、1つの「しかし」ではないにしても、 結果はほとんど私たちに合っています:ジャークは短い間隔で観察できます。

完全に正しいアニメーションを作成するには、別のCSS 3機能であるサイズの増加に頼る必要があります。 これを行うには、フレームの幅と高さを1ピクセルに設定し、transformプロパティを使用してスプライトを32ピクセルに増やします。 変換は要素自体のサイズだけでなく背景にも影響するため、サイズを幅3ピクセル、高さ1に設定し、アニメーション自体でも1ピクセルずつシフトします。 増加とともに、正確に32ピクセルになります。

Copy Source | Copy HTML



  1. .sprite
  2. {
  3. 位置絶対 ;
  4. 50% ;
  5. トップ33% ;
  6. 1px ; / *要素サイズを1ピクセルに設定* /
  7. 高さ1px ;
  8. マージン-16px 0 0 -16px ; / *変換によるインデントの増加は影響を受けません* /
  9. 背景urlsprite.pngno-repeat 0 0 ;
  10. 背景 サイズ3px 1px ; / *背景サイズも縮小されます* /
  11. -webkit-animation-name: スプライト ;
  12. -webkit-animation-duration:.3s;
  13. -webkit-animation-iteration-count: 無限 ;
  14. -webkit-animation-timing-function: リニア ;
  15. -webkit-transform:scaleX( 32 )scaleY( 32 ); / *要素のサイズを大きくします* /
  16. -webkit-transform-origin: 左上 ;
  17. }
  18. @ -webkit-keyframesスプライト
  19. {
  20. / *背景を1ピクセルずつシフト* /
  21. 0.000%
  22. {
  23. バックグラウンド位置-0px 0 ;
  24. }
  25. 25.000%
  26. {
  27. バックグラウンド位置-1px 0 ;
  28. }
  29. 50.000%
  30. {
  31. バックグラウンド位置-2px 0 ;
  32. }
  33. / *正直、私はなぜシフトする必要があるのか​​理解できませんでした* /
  34. / *もう1ピクセル(* /
  35. / *空のフレーム)。 それ以外の場合はすべて表示されません* /
  36. / *アニメーションフレーム。 * /
  37. 75.000%
  38. {
  39. バックグラウンド位置-3px 0 ;
  40. }
  41. 100,000%
  42. {
  43. バックグラウンド位置-0px 0 ;
  44. }
  45. }


現在、 結果は私たちの期待に完全に一致しています。 アニメーションは正常で、けいれんなどはありません。

キーフレームを生成するには、次の関数を使用できます。

Copy Source | Copy HTML



  1. 関数 generateKeyframes( $ count$ sprite_width
  2. {
  3. $ result = '' ;
  4. $カウント ++;
  5. for$ i = 0 ; $ i <= $ count ; $ i ++)
  6. {
  7. $ result 。= sprintf( "\ t%.3f %% \ n \ t {\ n \ t \ tbackground-position:-%dpx 0; \ n \ t} \ n"$ i * 100. 0 / $ count 、( $ i$ count )* $ sprite_width );
  8. }
  9. return $ result ;
  10. }


残念ながら、この方法はChromeやSafariなどのWebkitブラウザーでのみ機能するため、まだほとんど実用的ではありません。 Firefoxではアニメーションはスムーズなままで、Operaでは最初のフレームのみが表示され、IEではまったく機能しません。 そのため、JavascriptとFlashを使用したアニメーションは、しばらくの間その位置を保持します。



All Articles