简体   繁体   English

如何使用jQuery淡入列表中的下一个图像?

[英]How do I fade in the next image in the list using jQuery?

I have a list of images. 我有一张图片清单。 I would like to fade in the next image after the previous image has faded out. 上一张图像淡出后,我想在下一张图像中淡出。 I think it should be fairly simple.. 我认为应该相当简单。

<ul class="nav fading">
  <li><?= anchor("#","Supported by",array("class" => "footer-heading disabled")) ?></li>
  <li><?= img("assets/img/1.png") ?></li>
  <li><?= img("assets/img/2.png") ?></li>
</ul>

My code so far: 到目前为止,我的代码:

$(document).ready(function(){
    $(".fading img").hide(); //hide everything 
    $(".fading img:first").fadeIn(4000);
    $(".fading img:first").fadeOut(4000,function(){
        $(this).parent().next().fadeIn(4000);
    });
});

I've attempted to fade the same image in again, after it has been faded out and that works: 在淡出效果后,我试图再次淡入相同的图像:

$(document).ready(function(){
        $(".fading img").hide(); //hide everything 
        $(".fading img:first").fadeIn(4000);
        $(".fading img:first").fadeOut(4000,function(){
            $(this).fadeIn(4000);
        });
    });

The problem arises when I try to fade in the next image in the list. 当我尝试淡入列表中的下一张图像时,就会出现问题。

I would suggest the following 我建议以下

Replace this 取代这个

$(this).parent().next().fadeIn(4000);

With this 有了这个

$(this).parent().next().find('img').fadeIn(4000);

To find an <img> from the other, you'll have to traverse back down a " generation " with .find() or .children() , having gone up with .parent() : 要查找<img>从其他的,你必须与穿越背下来了“ .find().children()在经历了.parent()

$(this)             // the current, `:first` <img>
    .parent()       // to its parent <li> (2nd under the <ul>)
    .next()         // to the last <li>
    .find('img')    // to the <img> under the last <li>
    .fadeIn(4000);

Otherwise, the line was finding and attempting to .fadeIn() the <li> instead, which won't affect the <img> under it. 否则,该行将查找并尝试.fadeIn() <li> ,这不会影响其下的<img>

$(this).parent().next().fadeIn(4000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM