使用 jQuery 从动画开头加载动画图像
这是这篇文章的后续内容,它完全符合我需要它做的事情,除了有一个问题。
我使用 div 旋转图像,使用 setInterval 每 5 秒显示和隐藏一个 div。
这种方法的问题是,我正在加载动画 gif 文件,因此即使下一个图像切换,它也会变得混乱,因为图像一直都在那里并且一直在动画。
因此,发生的情况是 image1(div1) 将重新开始其动画,它将淡出,image2(div2) 将显示,但动画在不可见时已经开始。
我需要的是一种重新加载这些图像的方法,以便当下一个 div 淡入时动画再次从头开始。使用 Gif 执行此操作的唯一方法是实际从缓存加载它们,这就是我需要的解决方案使用 jQuery。
这就是我所拥有的:
var images = new Array ('.advert1', '.advert2');
var index = 0;
function rotateImage()
{
$(images[index]).fadeOut('fast', function()
{
index++;
if (index == images.length)
{
index = 0;
}
$(images[index]).fadeIn('fast');
});
}
setInterval (rotateImage, 5000);
在我的 html 中:
<div>
<div class="advert1"><img id="myImage" src="advert1.gif" alt="image test" /></div>
<div class="advert2"><img src="advert2.gif" alt="image test" /></div>
</div>
现在我需要的是能够使其“看起来”好像每次都加载新鲜的图像,以便当 div 淡入时,动画从头开始。
有人知道我该如何破解这个吗?
谢谢!
This is a follow on from this post which does exactly what I need it to do except there's a problem.
I am rotating images using divs which just show and hide a div every 5 seconds using setInterval.
The problem with this approach is that I am loading animated gif files so even when the next image switches, it becomes messy because the images have been there all along and been animating.
So, what happens is that image1(div1) will start fresh with its animation, it will fade out and image2(div2) will show but the animation would have already started when it was invisible.
What I need is a method of loading these images afresh so that the animation begins from the start again when the next div fades in. The only way to do this with Gifs is to actually load them from the cache and this is the solution I need using jQuery.
Here's what I have:
var images = new Array ('.advert1', '.advert2');
var index = 0;
function rotateImage()
{
$(images[index]).fadeOut('fast', function()
{
index++;
if (index == images.length)
{
index = 0;
}
$(images[index]).fadeIn('fast');
});
}
setInterval (rotateImage, 5000);
And in my html:
<div>
<div class="advert1"><img id="myImage" src="advert1.gif" alt="image test" /></div>
<div class="advert2"><img src="advert2.gif" alt="image test" /></div>
</div>
Now what I need is to be able to make it "seem" as though the images are loaded fresh every time so that when the div fades in, the animation begins from the start.
Anyone know how I can crack this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试通过再次设置 src 来重新加载图像:
did you tried to reload image by setting src again:
如果重新加载 src 属性,gif 会重新加载
If you reload the src property, the gif reload