使用 jQuery 从动画开头加载动画图像

发布于 2024-10-02 00:36:44 字数 1128 浏览 6 评论 0原文

这是这篇文章的后续内容,它完全符合我需要它做的事情,除了有一个问题。

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-10-09 00:36:44

您是否尝试通过再次设置 src 来重新加载图像:

var img = $(images[index] img);
//this should reset animation (but without proper caching it could be a pain)
img.attr(src,img.attr(src));
$(images[index]).fadeIn('fast');

did you tried to reload image by setting src again:

var img = $(images[index] img);
//this should reset animation (but without proper caching it could be a pain)
img.attr(src,img.attr(src));
$(images[index]).fadeIn('fast');
稀香 2024-10-09 00:36:44

如果重新加载 src 属性,gif 会重新加载

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]).get(0).src=$(images[index]).get(0).src;
   $(images[index]).fadeIn('fast');
 });
}

setInterval (rotateImage, 5000);

If you reload the src property, the gif reload

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]).get(0).src=$(images[index]).get(0).src;
   $(images[index]).fadeIn('fast');
 });
}

setInterval (rotateImage, 5000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文