【问题标题】:jQuery Continuous Rotator Multiple Elements at OncejQuery 一次连续旋转多个元素
【发布时间】:2012-11-14 09:00:38
【问题描述】:

我有这段代码,它可以很好地一次淡入和淡出一个 div。我需要一次淡出两个 div,并用接下来的两个替换它们。

$(function() {
    // Set first div to show
    $('.testimonials div:first').show();

    // Begin the loop, fade out, find next div, fade that div in, end the process and append back to main div.
    setInterval(function() {
        $('.testimonials div:first-child').fadeOut().next('div').fadeIn().end().appendTo('.testimonials');
    }, 5000);
)};

我尝试改进脚本:

淡入淡出工作 - 它会淡出前两个,然后淡出接下来的两个。但它永远不会循环!

$(function() {
    // Set first div to show
    $('.testimonials .quote:lt(2)').show();

    // Begin the loop, fade out, find next div, fade that div in, end the process and append back to main div.
    setInterval(function() {
        $('.testimonials .quote').slice(0,2).fadeOut().nextAll('.quote').slice(3,4).(.fadeIn().end().appendTo('.testimonials');
    }, 5000);
)};

淡入淡出工作 - 它会淡出前两个,然后淡出接下来的两个。但它永远不会循环!

【问题讨论】:

    标签: jquery continuous


    【解决方案1】:

    有点不同的方法:

    $(function () {
      (function anim(num, delay) {
        $('.testimonials .quote').slice(0, num).fadeIn().delay(delay).fadeOut().promise().done(function () {
          $('.testimonials').append(this);
          anim(num, delay);
        });
      }(2, 2000)); // change these values to fade another number of elements, or
                   // have a longer delay between the fades
    });
    

    演示:http://jsbin.com/ivuyex/5/

    【讨论】:

      【解决方案2】:

      使用 setInterval,您总是将前两个淡出,第三个和第四个淡入淡出。它确实循环,但在第一次迭代之后,就没有什么可以改变了。

      您可以在初始设置状态后尝试 fadeToggle():

      setInterval(function() {
          $('.testimonials .quote').fadeToggle().end().appendTo('.testimonials');
       }, 5000);
      

      【讨论】:

      • 嗯,有道理。我实际上是这样尝试的,它似乎正在工作,所有的褪色都不是很顺利。现在中间有一个“跳跃”。您可以在上面的 URL (layer3.waltercrow.co.nz) 上看到它的实际效果
      • 我明白你的意思。它需要先淡出,然后再淡入。 Yoshi 的解决方案非常适合。
      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2014-07-11
      相关资源
      最近更新 更多