考虑不使用 GIF,而是使用图像序列和 Javascript。我已经将一个粗略的可实例化 Javascript 对象组合在一起,它就是这样做的:
<html>
<head>
<script>
function Animator(target, frames, delay)
{
var _this = this;
// array of frames (image objects)
var f = [];
// a reference to the object where our frames will appear
var t = document.getElementById(target);
// the interval variable. Used to stop and start
var t_loop;
// create a new image element and remember it
var img_el = document.createElement("img");
// current frame
var c_frame = 0;
t.innerHTML = "";
t.appendChild(img_el);
var i, img;
// preload immediately
for(i = 0; i < frames.length; i++)
{
img = new Image();
img.src = frames[i];
f.push(img);
}
this.play = function()
{
t_loop = window.setInterval(_this.frame, delay);
}
this.stop = function()
{
clearInterval(t_loop);
}
this.gotoFrame = function(frame)
{
c_frame = frame;
_this.frame();
}
this.frame = function()
{
img_el.src = f[c_frame].src;
c_frame++;
if(c_frame >= f.length)
{
c_frame = 0;
}
}
}
function loaded()
{
var quip_frames = [
"http://www.abcteach.com/images/abma_thumb.gif",
"http://www.abcteach.com/images/abcu_thumb.gif",
"http://www.abcteach.com/images/zbma_thumb.gif",
"http://www.abcteach.com/images/zbcu_thumb.gif",
"http://www.abcteach.com/images/dnma_thumb.gif",
"http://www.abcteach.com/images/dncu_thumb.gif"
];
var anim1 = new Animator("target1", quip_frames, 100);
var anim2 = new Animator("target2", quip_frames, 100);
var anim3 = new Animator("target3", quip_frames, 100);
anim1.play();
window.setTimeout(anim2.play, 200);
window.setTimeout(anim3.play, 300);
}
</script>
</head>
<body onload="loaded()">
<div id="target1">
</div>
<div id="target2">
</div>
<div id="target3">
</div>
</body>
</html>
感谢ABCTeach的图片
该对象循环加载图像,将它们存储在一个数组中,然后通过简单地使用setInterval()
以所需速率交换帧。当页面加载时,它会生成三个这样的对象,每个都指向不同的 DIV,但每个都有相同的框架。然后它告诉每个播放器使用不同的延迟(第一个立即播放,第二个在 200 毫秒后,第三个在 300 毫秒后)。在您的情况下,您将为您使用的每个字母创建一个数组:
var A = [*array of URLs to images of the letter A*];
var B = [*array of URLs to images of the letter B*];
...
Animator
然后为页面上的每个 div创建一个新对象,并将其发送到适当的字母数组:
var letter_so_and_so = new Animator("div_so_and_so", A, 100);
var letter_this_and_that = new Animator("div_this_and_that", B, 100);
var another_letter = new Animator("another_div", B, 100);
最后给每个人一个偏移开始,或者调用他们的Animator.gotoFrame(f)
方法在不同的帧上开始(不要忘记Animator.play()
!)。
JSFiddle
尝试在你的代码中采用这样的东西,我想你会发现它会产生奇迹。