jquery 褪色画廊

发布于 2024-08-10 00:40:35 字数 1440 浏览 6 评论 0原文

大家好,我用 jQuery 写了一个简单的淡入淡出画廊。它基本上循环播放一组图像,从一个图像淡入另一个图像。正如预测的那样,它工作得很好,直到到达最后一个图像,然后它不会从最后一个图像淡出到第一个图像,只是为了显示它。

这是我的 jQuery

$(document).ready(function(){

Zindex = 99999;
i = 0;

$(".flash img").each(function(){
    $(this).css({
        'position':'absolute',
        'z-index':Zindex,
        'display':'none'
    });
    Zindex--;
});

$(".flash img:first").show();

doFade = function(){        
    if( i == 6 ) { i = 0; };        
    $(".flash img:eq("+parseInt(i+1)+")").fadeIn(100);
    $(".flash img:eq("+parseInt(i)+")").fadeOut(1000);      
    setTimeout("doFade()", 2000);
    i++;
};

doFade();

});

注意:只会有 7 张图像。

我的标记

<div class='flash'>
    <img src="img/gallery/picture1.jpg" width="780" height="253" alt="Picture1">
    <img src="img/gallery/picture2.jpg" width="780" height="253" alt="Picture2">
    <img src="img/gallery/picture3.jpg" width="780" height="253" alt="Picture3">
    <img src="img/gallery/picture4.jpg" width="780" height="253" alt="Picture4">
    <img src="img/gallery/picture5.jpg" width="780" height="253" alt="Picture5">
    <img src="img/gallery/picture6.jpg" width="780" height="253" alt="Picture6">
    <img src="img/gallery/picture7.jpg" width="780" height="253" alt="Picture7">
</div> 

我认为问题出在这一行

if( i == 6 ) { i = 0; };

Hay all, i've wrote a simple fading gallery with jQuery. It basically loops though a set of images, fading from one to another. It works perfectly, as predicted, until it gets to the last image, it then doesn't fade from the last to the first, just to displays it.

here's my jQuery

$(document).ready(function(){

Zindex = 99999;
i = 0;

$(".flash img").each(function(){
    $(this).css({
        'position':'absolute',
        'z-index':Zindex,
        'display':'none'
    });
    Zindex--;
});

$(".flash img:first").show();

doFade = function(){        
    if( i == 6 ) { i = 0; };        
    $(".flash img:eq("+parseInt(i+1)+")").fadeIn(100);
    $(".flash img:eq("+parseInt(i)+")").fadeOut(1000);      
    setTimeout("doFade()", 2000);
    i++;
};

doFade();

});

note: there's only ever going to be 7 images.

And my markup

<div class='flash'>
    <img src="img/gallery/picture1.jpg" width="780" height="253" alt="Picture1">
    <img src="img/gallery/picture2.jpg" width="780" height="253" alt="Picture2">
    <img src="img/gallery/picture3.jpg" width="780" height="253" alt="Picture3">
    <img src="img/gallery/picture4.jpg" width="780" height="253" alt="Picture4">
    <img src="img/gallery/picture5.jpg" width="780" height="253" alt="Picture5">
    <img src="img/gallery/picture6.jpg" width="780" height="253" alt="Picture6">
    <img src="img/gallery/picture7.jpg" width="780" height="253" alt="Picture7">
</div> 

I think the problem lies in the line

if( i == 6 ) { i = 0; };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谁对谁错谁最难过 2024-08-17 00:40:35

当您到达“循环”末尾并执行时,会发生两件事:

if( i == 6 ) { i = 0; };

首先,您会淡入 i + 1,这是第二个图像,而不是第一个,因为 i 现在是 0。

其次,您正在淡出第一个图像 i,它甚至不可见。当 i + 1 为 6 时,显示的最后一张图像是图像 7。

因此,您永远不会隐藏最后一张图像,也永远不会显示第一张图像。对我来说,一个非常快速的解决方案是使淡出语句首先执行(i 仍然“指向”当前图像)并设置 i = -1 而不是 0 code> 当“循环”结束时。

doFade = function(){
    $(".flash img:eq("+parseInt(i)+")").fadeOut(1000); 
    if( i == 6 ) { i = -1; }
    $(".flash img:eq("+parseInt(i+1)+")").fadeIn(100);             
    setTimeout("doFade()", 2000);
    i++;
};

然而,当开始循环时,这似乎确实中断了转换流程(我猜是因为淡入淡出语句现在由逻辑分隔)。一个简单的修复应该很简单,只需引入 currentnext 变量并在开始时使用这些变量在淡入淡出语句中执行所有逻辑即可。

Two things are happening when you reach the end of the 'loop' and execute:

if( i == 6 ) { i = 0; };

First, you're fading in i + 1, which is the second image, not the first, because i is now 0.

Second, you are fading out i, the first image, which isn't even visible. That last image being displayed was image 7, when i + 1 was 6.

So you're never hiding the last image, and never showing the first image. A really quick solution for me was to make the fade out statement the first to execute (i is still 'pointing' to the current image) and setting i = -1 instead of 0 when the 'loop' ends.

doFade = function(){
    $(".flash img:eq("+parseInt(i)+")").fadeOut(1000); 
    if( i == 6 ) { i = -1; }
    $(".flash img:eq("+parseInt(i+1)+")").fadeIn(100);             
    setTimeout("doFade()", 2000);
    i++;
};

However, this does seem to interrupt the flow of the transitions when starting the loop over (I guess because the fade statements are now separated by logic). A simple fix for that should be as easy as introducing current and next variables and doing all logic at the beginning, using those variables in the fade statements.

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