【问题标题】:jQuery cycle pugin and infinite loopjQuery循环pugin和无限循环
【发布时间】:2012-01-04 18:30:17
【问题描述】:

我正在使用THIS 插件来显示每 5 秒向上滚动的“n”个字符串。

特别是你可以看到底部HERE的例子。

这个插件每次只显示一个元素,所以我做了这个显示 7 个元素的拆分函数:

$.fn.splitUp = function (splitBy, wrapper) {
    $all = $(this).find('>*');
    var fragment = Math.ceil($all.length / splitBy);
    for (i = 0; i < fragment; i++)
        $all.slice(splitBy * i, splitBy * (i + 1)).wrapAll(wrapper);
    return $(this);
}

$('#list').splitUp(7, '<li><li/>').cycle({
    fx: 'scrollUp',
    pause: 1,
    timeout: 5000,
    speed: 2000
});

效果很好。

问题是:如果我有 9 个字符串,它会显示前 7 个字符串,而不是只有 2 个字符串... 我想显示 7 个字符串,而不是 2 + 5。一个无限循环。

这怎么可能?

谢谢

编辑 http://jsfiddle.net/QZu2Z/

【问题讨论】:

  • 你能用你所拥有的东西制作一个小提琴,它会更容易使用。
  • 我试着搞砸了几次,但没有运气;我可以告诉你它应该可以完成并且你需要使用before 事件;与它一起传递了一个选项属性对象,并且该对象是一个addSlide() 方法,用于在幻灯片运行时动态地将新幻灯片添加到幻灯片中。看看这个页面jquery.malsup.com/cycle/more.html?v2.23,它的中间有 3 个标有“将幻灯片添加到正在运行的幻灯片”的演示。基本上,您需要计算在下一张幻灯片上显示哪些字符串,并使用addSlide() 将它们动态添加到幻灯片中。
  • 看看这里:jsfiddle.net/QZu2Z/2 我以不同的方式做了,但在循环方面没有运气,但它肯定是一种更简单的方法......也许你比我更幸运解决它现在。谢谢。

标签: jquery plugins loops cycle


【解决方案1】:

这是使用before 事件和事件传递的options 属性可用的addSlide() 方法的解决方案。我在代码中添加了 cmets 来解释发生了什么。

代码(小提琴演示:http://jsfiddle.net/QZu2Z/4/

var pos = 0;
var count = 1;
var maxItems = 7;
var items = [];

function createSlide()
{
    // This function takes the array of items we have and builds a new ul
    // list based on maxitems and last pos, the result is multiple calls
    // to this function produces output like so:

    //  First pass returns: 1 2 3 4 5 6 7
    // Second pass returns: 8 9 1 2 3 4 5
    //  Third pass returns: 6 7 8 9 1 2 3
    //  Forth pass returns: 4 5 6 7 8 9 1
    // and so forth...

    var elem = $('<ul class="list"></ul>');

    for(i = 1; i <=9; i++)
    {
        if (pos >= 9)
        {
            pos = 0;
        }

        if(count <= maxItems)
        {
            count = count + 1;

            $(elem).append(items[pos]);

            pos = pos + 1;
        }
    }

    count = 1;

    return elem;
}

function onBefore(curr, next, opts) {
    // This is the onBefore slide event; we're simply checking if the addSlide
    // method is available on the opts object as apparently on the first pass,
    // addSlide is undefined (plugin hasn't yet created the function);

    if (!opts.addSlide)
    {
        return;
    }

    // addSlide function is available so generate the next slide and add it
    opts.addSlide(createSlide());
}

$(document).ready(function() {
    // Generate an array of items to use in our cycle
    $(".wrapper li").each(function(i) {
        items[i] = $(this).clone().wrap('<div>').parent().html();
    });

    // Grab the slideshow object
    var $ss = $('.wrapper');

    // Erase the contents, we have the data stored in the array [items]
    $ss.empty();

     // Create new slide and append it to our object, results in 1 2 3 4 5 6 7
    $ss.append(createSlide());

    // Create another slide and append it to our object, results 8 9 1 2 3 4 5
    $ss.append(createSlide());

    // start the slideshow (a slideshow can't be started unless there's 2 
    // slides hence why we created two slides above to start with).  The
    // before/onBefore event will add a new slide every cycle of the slideshow.
    $ss.cycle({
        fx: 'scrollUp',
        pause: 1,
        timeout: 5000,
        speed: 2000,
        before: onBefore
    });
});

额外说明:

要记住的一件事虽然对大多数人来说应该不是问题,但由于这确实会在每个循环中添加一张新幻灯片,因此长时间在页面上打开浏览器会开始吸收更多内容随着新元素不断被添加到 DOM 中,还有更多资源。但是,它也有一个修复程序,当您循环播放以将它们循环回原始起点时,它也可以解决,然后不需要添加新幻灯片并且可以循环播放它们;在视觉上这更容易看到然后解释

 -> createSlide() = 1 2 3 4 5 6 7 (store this first results to compare with rest)
|   createSlide() = 8 9 1 2 3 4 5
|   createSlide() = 6 7 8 9 1 2 3
|   createSlide() = 4 5 6 7 8 9 1
|   createSlide() = 2 3 4 5 6 7 8
|   createSlide() = 9 1 2 3 4 5 6
|   createSlide() = 7 8 9 1 2 3 4
|   createSlide() = 5 6 7 8 9 1 2
 -< createSlide() = 3 4 5 6 7 8 9
    createSlide() = 1 2 3 4 5 6 7 (this value equals the first result so no new 
slides need to be added, let the process use the slides that are already added
and loop back to the beginning

额外注意事项:

我刚刚意识到的另一件事是,如果您使用上述技术,那么从技术上讲,您可以摆脱 before 事件回调,并在开始幻灯片放映之前简单地生成所有需要的幻灯片。

【讨论】:

  • 我刚刚添加了一个新的var,var listItems = $('.list li').length;动态获取列表的长度,用 listItems 替换“9”。它有效,+1。
  • 很高兴我们找到了一个可行的解决方案,我刚刚更新了我的答案,提供了一些可能有用的更多信息。
猜你喜欢
  • 2012-11-06
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2015-06-25
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多