我期待創建一個加載精靈,它將在所有瀏覽器中工作,因爲當前IE將在調用新頁面時停止.gif文件中的動畫。如何使用背景位置創建加載圖像精靈
到目前爲止,我有這個功能:
counter = 1; //how many slides have we seen
function loadingWheel(loc) {
var img = $('.img_working'), //this is the div with the sprite
slideCount = 76, //this is so that we know when to stop
slideH = 32, //this is the height of each frame in the sprite
pos = loc, //this is the current y position
newPos = pos + slideH; //now we increase the position to set the css
//set the new css position
img.css({ 'background-position': 'center -' + newPos + 'px' });
//Add to the count
counter++;
//if we are the the last slide, we are going to reset the counter
if (counter == slideCount) {
//reset the position, wait for 700 ms then go again
setTimeout(loadingWheel(0), 700);
}
else {
//wait for 700 ms then go again
setTimeout(loadingWheel(newPos), 700);
}
};
loadingWheel(0);
的想法是使用setTimeout
創建一個循環,這將在目前的位置,然後提高它傳遞,然後再打電話。
的HTML到目前爲止CSS很簡單:
<div id="workingMessage">
<div class="img_working"></div>
</div>
.img_working {
width:32px;
height:32px;
margin:0 auto 20px;
background: url(http://i.imgur.com/Hwgkxhy.png) top center no-repeat;
}
,這裏是一個小提琴顯示我已經這麼遠:
我有一種感覺,動畫由於我設置背景位置的方式存在問題而無法工作 - 以前是否有其他人做過類似的事情? 我已經看過其他的插件,但我希望迄今爲止我所需要的幾乎都是我需要的,因此我不想包含第三方插件。
你確實意識到瀏覽器仍然阻止了很多類型的請求,這就是爲什麼大多數人都仍在使用舊的動畫GIF而不是JS動畫? – adeneo
是的,此刻我正在使用一個.gif,除了IE以外,它在其他所有方面都運行良好。如果可能的話,我希望能夠同時運行一些功能。 – wf4
你想實現什麼?我看到一個名爲「Wheel」的函數,但沒有任何三角函數,所以你所有的點都可以做到最好。你錯過了一些分號。也不會做任何事情,它應該是全球性的。 – efkah