27

divの背景位置をゆっくりとアニメーション化しようとしていますが、ぎくしゃくした動きはありません。ここで私の現在の努力の結果を見ることができます:

http://jsfiddle.net/5pVr4/2/

@-webkit-keyframes MOVE-BG {
    from {
        background-position: 0% 0%
    }
    to { 
        background-position: 187% 0%
    }
}

#content {
    width: 100%;
    height: 300px;
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat;
    text-align: center;
    font-size: 26px;
    color: #000;

    -webkit-animation-name: MOVE-BG;
    -webkit-animation-duration: 100s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
}

私は何時間もこれに取り組んできましたが、サブピクセルレベルでゆっくりとスムーズにアニメーション化するものを見つけることができません. 私の現在の例は、このページのサンプル コードから作成されました: http://css-tricks.com/parallax-background-css3/

私が求めているアニメーションの滑らかさは、このページの translate() の例で見ることができます:

http://css-tricks.com/tale-of-animation-performance/

background-position で実行できない場合、複数の div で繰り返し背景を偽造し、translate を使用してそれらの div を移動する方法はありますか?

4

3 に答える 3

11

background-position をアニメーション化すると、パフォーマンスの問題が発生します。ブラウザーは、変換を含め、変換プロパティを非常に安価にアニメーション化します。

これは、translate を使用して無限スライド アニメーション (プレフィックスなし) を使用した例です。

http://jsfiddle.net/brunomuller/5pVr4/504/

@-webkit-keyframes bg-slide {
    from { transform: translateX(0); }
    to { transform: translateX(-50%); }
}

.wrapper {
    position:relative;
    width:400px;
    height: 300px;
    overflow:hidden;
}

.content {
    position: relative;
    text-align: center;
    font-size: 26px;
    color: #000;
}

.bg {
    width: 200%;
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) repeat-x;
    position:absolute;
    top: 0;
    bottom: 0;
    left: 0;
    animation: bg-slide 20s linear infinite;
}
于 2015-07-23T19:05:41.567 に答える
2

HTMLとCSSを少し調整する必要があります

ワーキングデモ

HTML

<div id="wrapper">
    <div id="page">
    Foreground content
</div>

<div id="content"> </div>
</div>

CSS

@-webkit-keyframes MOVE-BG {
    from { left: 0; }
    to { left: -2000px; }
}

#wrapper {
    position:relative;
    width:800px;
    height: 300px;
    overflow:hidden;
}

#page {
    text-align: center;
    font-size: 26px;
    color: #000;
}

#content {
    width: 2000px;
    height: 300px;
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat;
    position:absolute;
    top: 0;
    left: 0;
    z-index:-1;
    -webkit-animation-name: MOVE-BG;
    -webkit-animation-duration: 100s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
}
于 2014-01-13T11:39:05.837 に答える