简体   繁体   English

如何在jQuery动画中连续旋转孩子?

[英]How to continuously rotate children in a jQuery animation?

I have a div with class 'bannergroup' that contains multiple divs 'banneritem'. 我有一个带有'bannergroup'类的div,它包含多个div'banneritem'。 I want these items to rotate (fade in then fade out) in place of each other. 我希望这些项目能够旋转(淡入然后淡出)代替彼此。

I can have several divs with the class bannergroup and each one should rotate separately. 我可以使用类bannergroup有几个div,每个div应该单独旋转。

Here is the HTML: 这是HTML:

<div class="bannergroup">
  <div class="banneritem">Only visible one at a time</div>
  <div class="banneritem">Only visible one at a time</div>
  <div class="banneritem">Only visible one at a time</div>
  <div class="banneritem">Only visible one at a time</div>
</div>

<div class="bannergroup">
  <div class="banneritem">Only visible one at a time</div>
  <div class="banneritem">Only visible one at a time</div>
  <div class="banneritem">Only visible one at a time</div>
  <div class="banneritem">Only visible one at a time</div>
</div>

My Jquery looks like: 我的Jquery看起来像:

$('.banneritem').css('display', 'none');
$('.bannergroup').children('.banneritem').each(function( i ) {
  $(this).fadeIn().delay(4000).fadeOut();               
});

The problem: the each statement continues to run before the previous div completes. 问题:每个语句在前一个div完成之前继续运行。 I want it to wait until the previous child is gone. 我希望它等到上一个孩子不见了。 Also, I need this to continuously run. 此外,我需要这个继续运行。 After a single time it stops. 一次后它停止。 I can put this into a function, but I am not sure how to know to call it again. 我可以把它放到一个函数中,但我不知道怎么知道再次调用它。

EDIT : There are not always 4 child items. 编辑 :并不总是4个子项目。 Also one group may have a different number of children than the others, but they should both rotate in-sync. 另外一组可能具有与其他组不同的子组数,但它们应该同步旋转。 It is ok if one completes before the other and then just restarts itself. 如果一个在另一个之前完成然后重新启动它就可以了。

I have answered this question multiple times before. 我已经回答了这个问题 的时间了。 This time I will try wrapping it in a jQuery plugin. 这次我将尝试将其包装在jQuery插件中。 The .rotate() function will apply the effect you want to the children of the matched elements, a fade in/out effect per children in a continuous animation. .rotate()函数将您想要的效果应用于匹配元素的子元素,即连续动画中每个子元素的淡入/淡出效果。

$.fn.rotate = function(){
  return this.each(function() {

    /* Cache element's children */
    var $children = $(this).children();

    /* Current element to display */
    var position = -1;

    /* IIFE */
    !function loop() {

        /* Get next element's position.
         * Restarting from first children after the last one.
         */
        position = (position + 1) % $children.length;

        /* Fade element */
        $children.eq(position).fadeIn(1000).delay(1000).fadeOut(1000, loop);
    }();
  });
};

Usage: 用法:

$(function(){
  $(".banneritem").hide();
  $(".bannergroup").rotate();
});  

See it here. 在这里看到它。

jsFiddle example jsFiddle例子

$('div.bannergroup').each(function () {
    $('div.banneritem', this).not(':first').hide();
    var thisDiv = this;
    setInterval(function () {
        var idx = $('div.banneritem', thisDiv).index($('div.banneritem', thisDiv).filter(':visible'));
        $('div.banneritem:eq(' + idx + ')', thisDiv).fadeOut(function () {
            idx++;
            if (idx == ($('div.banneritem', thisDiv).length)) idx = 0;
            $('div.banneritem', thisDiv).eq(idx).fadeIn();
        });
    }, 2000);
});

You can solve this problem in 2 ways. 您可以通过两种方式解决此问题。 The one below is the easiest, using the index to increase the delay per item. 下面的一个是最简单的,使用索引来增加每个项目的延迟。

$('.banneritem').css('display', 'none');
$('.bannergroup').children('.banneritem').each(function( i ) {
  $(this).delay(4000 * i)).fadeIn().delay(4000 * (i+1)).fadeOut();               
});

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

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