2014-01-13 96 views
14

我試圖動畫一個div的背景位置,慢慢地,但不必急動它。你可以看到我這裏目前努力的結果:CSS動畫背景,地位與流暢的效果(子像素動畫)

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/

動畫的平滑度可以在此頁面的翻譯()的例子可以看出以後我:

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

如果可以」不要用背景位置來完成,有沒有辦法用多個div來僞造重複背景並使用translate來移動這些div?

+0

你知道WebKit瀏覽器這僅適用嗎? –

+0

@php_nub_qq是的,翻譯更兼容。例如。 http://www.w3schools.com/css/css3_2dtransforms.asp –

回答

22

結帳這個例子:

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

<div id="content">Foreground content 
    <div class="bg"></div> 
</div> 

@-webkit-keyframes MOVE-BG { 
    from { 
    -webkit-transform: translateX(0); 
    } 
    to { 
    -webkit-transform: translateX(-187%); 
    } 
} 

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

.bg{ 
    position: absolute; 
    left: 0; 
    right: 0; 
    top: 0; 
    bottom: 0; 
    z-index: -1; 
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat; 

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

是的,這看起來很棒!謝謝。它怎麼能一直重複?彷彿背景有無限的寬度,並繼續前進? –

+3

http://jsfiddle.net/5pVr4/6/ - 更新源,結賬 –

+0

我認爲我們只是工作的非常相似,達到同樣的效果了一句:這麼多http://jsfiddle.net/5pVr4/7/謝謝!棒極了。 –

2

你應該調整你的HTML和CSS點點

Working Demo

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; 
} 
+0

不幸的是,結果看起來很生澀。感謝您發佈解決方案。 –

3

動畫背景位置會造成一些性能問題。瀏覽器將以更便宜的方式動畫變換屬性,包括翻譯。

這裏使用一個例子翻譯爲無限幻燈片動畫(無前綴):

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; 
} 
+0

嗨布魯諾,我怎麼編輯這個移動也在一個對角線?所以從右到左像現在這樣在同一個方向上加上一個稍微的對角線? – user2513846

+0

你可以做這樣的事情:http://jsfiddle.net/5pVr4/723/ 我使用方形圖案和動畫單個元素,所以對角線動畫將水平和垂直相同。如果您希望水平移動速度更快,垂直移動速度更慢,則可以嵌套元素,併爲每個元素使用不同的時間。其他可能性是使用具有不同寬高比的圖案。 –