今すぐ以下の方法が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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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 ; } }
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
- @ -webkit-keyframesスプライト
- {
- 0%
- {
- バックグラウンド位置 : -0px 0 ;
- }
- 33.332%
- {
- バックグラウンド位置 : -0px 0 ;
- }
- 33.334%
- {
- background-position : -32px 0 ;
- }
- 66.665%
- {
- background-position : -32px 0 ;
- }
- 66.667%
- {
- バックグラウンド位置 : -64px 0 ;
- }
- 99.999%
- {
- バックグラウンド位置 : -64px 0 ;
- }
- 100%
- {
- バックグラウンド位置 : -0px 0 ;
- }
- }
コードは非常に膨大であることが判明しましたが、1つの「しかし」ではないにしても、 結果はほとんど私たちに合っています:ジャークは短い間隔で観察できます。
完全に正しいアニメーションを作成するには、別のCSS 3機能であるサイズの増加に頼る必要があります。 これを行うには、フレームの幅と高さを1ピクセルに設定し、transformプロパティを使用してスプライトを32ピクセルに増やします。 変換は要素自体のサイズだけでなく背景にも影響するため、サイズを幅3ピクセル、高さ1に設定し、アニメーション自体でも1ピクセルずつシフトします。 増加とともに、正確に32ピクセルになります。
Copy Source | Copy HTML
- .sprite
- {
- 位置 : 絶対 ;
- 左 : 50% ;
- トップ : 33% ;
- 幅 : 1px ; / *要素サイズを1ピクセルに設定* /
- 高さ : 1px ;
- マージン : -16px 0 0 -16px ; / *変換によるインデントの増加は影響を受けません* /
- 背景 : url ( sprite.png ) no-repeat 0 0 ;
- 背景 サイズ : 3px 1px ; / *背景サイズも縮小されます* /
- -webkit-animation-name: スプライト ;
- -webkit-animation-duration:.3s;
- -webkit-animation-iteration-count: 無限 ;
- -webkit-animation-timing-function: リニア ;
- -webkit-transform:scaleX( 32 )scaleY( 32 ); / *要素のサイズを大きくします* /
- -webkit-transform-origin: 左上 ;
- }
- @ -webkit-keyframesスプライト
- {
- / *背景を1ピクセルずつシフト* /
- 0.000%
- {
- バックグラウンド位置 : -0px 0 ;
- }
- 25.000%
- {
- バックグラウンド位置 : -1px 0 ;
- }
- 50.000%
- {
- バックグラウンド位置 : -2px 0 ;
- }
- / *正直、私はなぜシフトする必要があるのか理解できませんでした* /
- / *もう1ピクセル(* /
- / *空のフレーム)。 それ以外の場合はすべて表示されません* /
- / *アニメーションフレーム。 * /
- 75.000%
- {
- バックグラウンド位置 : -3px 0 ;
- }
- 100,000%
- {
- バックグラウンド位置 : -0px 0 ;
- }
- }
現在、 結果は私たちの期待に完全に一致しています。 アニメーションは正常で、けいれんなどはありません。
キーフレームを生成するには、次の関数を使用できます。
Copy Source | Copy HTML
- 関数 generateKeyframes( $ count 、 $ sprite_width )
- {
- $ result = '' ;
- $カウント ++;
- for ( $ i = 0 ; $ i <= $ count ; $ i ++)
- {
- $ result 。= sprintf( "\ t%.3f %% \ n \ t {\ n \ t \ tbackground-position:-%dpx 0; \ n \ t} \ n" 、 $ i * 100. 0 / $ count 、( $ i % $ count )* $ sprite_width );
- }
- return $ result ;
- }
残念ながら、この方法はChromeやSafariなどのWebkitブラウザーでのみ機能するため、まだほとんど実用的ではありません。 Firefoxではアニメーションはスムーズなままで、Operaでは最初のフレームのみが表示され、IEではまったく機能しません。 そのため、JavascriptとFlashを使用したアニメーションは、しばらくの間その位置を保持します。