2016-11-17 72 views
1

我有一個精靈表是5760像素寬度5400像素高度包含10行和6列每個是960像素寬度540像素高度。精靈表中的所有精靈都是從左到右,從上到下排列的。我想把60張圖片作爲一個可能使用CSS的動畫來播放,但我很難理解需要什麼代碼。有沒有人有什麼建議?協助CSS精靈表動畫

+0

所有你需要做的就是操縱'背景position'。你搜索了什麼嗎? – slick

+0

我一直在Google上搜尋一個多星期試圖看看別人如何使用CSS從精靈表創建動畫,但由於某種原因,我無法讓它與我的PNG文件一起工作。我也無法找到任何使用網格精靈表使用CSS動畫的人。大多數人使用精靈表作爲單行或列而不是網格。 –

+0

我在這裏實現了一個類似的東西:http://templates.newsoftdemo.info/steps-animation –

回答

0

最簡單的解決方案是按順序分配CSS類的背景位置更改。

即。

.class1{background-position:0,0} // top left 
.class2{background-position:-960px, 0} // indent by 960 
// go to .class6 minus 960 to first px value 
.class7{background-position:0,540px} // got to left of 2nd row 
.class8{background-position:-960px,-540px} // got to left of 2nd row 
// keep going for 10 rows, minus 540 per row and 960 per col 

然後在JavaScript(用jQuery)

var viewarea = documentGetElementByID('my_window'); // id of DIV 
var counter = 0; 
var speed = 300; // miliseconds between frame change 

function loopAnimation(){ 
    // remove old frame 
    if($(viewarea).hasClass("class"+counter){ 
    $(viewarea).removeClass("class"+counter); 
    { 
    // change counter 
    if(counter == 60){ 
    counter = ; 
    }else { 
    counter++; 
    } 
    // move to next frame 
    $(viewarea).addClass("class"+counter); 
    // set animation to do another frame 
    var t = setTimeout(function(){loopAnimation();}, speed); 
} 
// begin animation 
loopAnimation();