質問

このコードを使用したいのですが、アニメーションが垂直になるほうが欲しいです。

header {
    width: 100%;
    height: 150px;
    background-image: url(http://www.scottishheritageusa.org/imgs/header_separator.png); //replace with your image
    background-position: 0px;
    background-repeat: repeat-x;
    -webkit-animation: myanim 5s infinite linear;
       -moz-animation: myanim 5s infinite linear;
         -o-animation: myanim 5s infinite linear;
            animation: myanim 5s infinite linear;
}

@-webkit-keyframes myanim {
    0%   { background-position: 0px; }
    100% { background-position: 100px; } /* set this to the width of the image */
}
@-moz-keyframes myanim {
    0%   { background-position: 0px; }
    100% { background-position: 100px; } /* set this to the width of the image */
}
@-o-keyframes myanim {
    0%   { background-position: 0px; }
    100% { background-position: 100px; } /* set this to the width of the image */
}
@keyframes myanim {
    0%   { background-position: 0px; }
    100% { background-position: 100px; } /* set this to the width of the image */
}
.

私はここにこのコードを見つけました: http://jsfiddle.net/e3wld/3/

助けてくれてありがとう

役に立ちましたか?

解決

背景位置は2つのパラメータを受け入れる:

  • 水平位置決め用の最初のもの
  • と垂直位置決め用の2番目のもの

w3schools

の背景位置に関する詳細

SO、Background-Positionプロパティに2番目のパラメータを追加することで あなたの背景を垂直にアニメートします。

jsfiddle

これに:

header {
width: 100%;
height: 131px;
background-image: url(http://www.scottishheritageusa.org/imgs/header_separator.png);
background-position: 0px 0px;
background-repeat: repeat-y;
-webkit-animation: myanim 5s infinite linear;
   -moz-animation: myanim 5s infinite linear;
     -o-animation: myanim 5s infinite linear;
        animation: myanim 5s infinite linear;
}

@-webkit-keyframes myanim {
  0%   { background-position: 0px; }
  100% { background-position: 0px 1000px; } /* set this to the width of the image */
}
@-moz-keyframes myanim {
  0%   { background-position: 0px; }
  100% { background-position: 0px 1000px; } /* set this to the width of the image */
}
@-o-keyframes myanim {
  0%   { background-position: 0px; }
  100% { background-position: 1000px; } /* set this to the width of the image */
}
@keyframes myanim {
  0%   { background-position: 0px; }
  100% { background-position: 0px 1000px; } /* set this to the width of the image */
}
.

他のヒント

要素90度を変換することによって垂直方向をすることができます。

transform: rotate(90deg);
-webkit-transform: rotate(90deg);
.

作業フィドル

バックグラウンド位置値の1つだけを設定してアニメートしているため、最初の1つ(X-POSITION)と他のデフォルトを参照していると仮定します(私は思う)。

これを試してください

** jsfiddle demo **

css

header {
    width: 100%;
    height: 131px;
    background-image: url(http://www.scottishheritageusa.org/imgs/header_separator.png);
    background-position: 0px 0px; /* note */
    background-repeat: repeat-x;
    -webkit-animation: myanim 5s infinite linear;
       -moz-animation: myanim 5s infinite linear;
         -o-animation: myanim 5s infinite linear;
            animation: myanim 5s infinite linear;
}

@-webkit-keyframes myanim {
  0%   { background-position: 0px 0px; }
  100% { background-position: 0px 1000px; } 
}
@-moz-keyframes myanim 
{  0%   { background-position: 0px 0px; }
  100% { background-position: 0px 100px; } 
}
@-o-keyframes myanim {
  0%   { background-position: 0px 0px; }
  100% { background-position: 0px 100px; } 
}
@keyframes myanim {
  0%   { background-position: 0px 0px; }
  100% { background-position: 0px 100px; } 
}
.

このように垂直回転に設定したい場合: http://jsfiddle.net/jme11/aykc2/

次に、繰り返しをrepeat-yに設定し、要素の高さに合わせて背景位置を変更する必要があります。

@keyframes myanim {
  0%   { background-position: 0px 0px; }
  100% { background-position: 0px 131px; } /* set this to the height of the image */
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top