简体   繁体   English

jQuery cycle2以编程方式逐步加载

[英]jQuery cycle2 progressive loading programmatically

I am using cycle2, and set it up programmatically in a separate scripts.js file. 我正在使用cycle2,并以编程方式在单独的scripts.js文件中进行设置。 Up to the point before adding progressive loading, the sliders all work well. 直到添加渐进式加载之前,所有滑块都可以正常工作。 I have multiple sliders on the page, so have set this up as follows: 我在页面上有多个滑块,因此将其设置如下:

$('.slider').each(function () {
    var $this = $(this);
    $this.cycle({
        fx: 'scrollHorz',
        slides: '> a',
        sync: true,
        progressive: slides,
        speed: 'fast',
        timeout: 0,
        autoHeight: 'container',
        next: $this.next('.slider-navigation').find('.next'),
        prev: $this.next('.slider-navigation').find('.prev'),
        loader: true
    });
});

And my HTML markup is, for each slider, as an example: 以每个滑块为例,我的HTML标记是:

<div class="slider">
    <a href="/">
        <img src="example.jpg">
    </a>
    <script class="other-slides" type="text/cycle">
        var slides = [
            "<a href=" / "><img src="
            another - example.jpg " /></a>",
            "<a href=" / "><img src="
            another - example.jpg " /></a>",
            "<a href=" / "><img src="
            another - example.jpg " /></a>"
        ];
    </script>
</div>

However, now when I load the page, my console states: ReferenceError: slides is not defined which makes sense as the cycle init is in script.js and this markup is on another page, but how can I possibly make this work, or is there a better way? 但是,现在,当我加载页面时,我的控制台将声明: ReferenceError: slides is not defined ,这是有意义的,因为cycle init在script.js中并且此标记在另一个页面上,但是我如何才能使该工作正常进行,或者有更好的方法吗? Remember there's multiple sliders on the page. 请记住,页面上有多个滑块。

Thanks, R 谢谢,R

In the Cycle 2 progressive demo there's an ID on the script tag which is used to reference the additional slides to load progressively. Cycle 2渐进式演示中 ,脚本标签上有一个ID,用于引用要逐步加载的其他幻灯片。 Obviously you can't use an ID because you're dealing with multiple slideshows. 显然,由于要处理多个幻灯片,因此无法使用ID。 At first I thought you could just apply a similar approach to how you've handled the prev and next controls. 最初,我认为您可以对上一个和下一个控件采用类似的方法。 However if you look at line 1374 of the Cycle 2 source you'll see that it's using the $() function and is just expecting a selector string. 但是,如果您看一下Cycle 2源代码的1374行,您会发现它正在使用$()函数,并且只是期望选择器字符串。 Fortunately it will also accept a function which will allow us to mimic the approach used when passing a string. 幸运的是,它还将接受一个函数,该函数将允许我们模仿传递字符串时使用的方法。

You need to watch the quoting of attributes in your JSON data. 您需要注意JSON数据中属性的引用。 Each slide should be wrapped in double quotes with single quotes used for attributes. 每张幻灯片都应使用双引号括起来,并在属性中使用单引号。 Also it won't work if you assign the JSON to a variable in your script tag. 如果您将JSON分配给脚本标签中的变量,也将无法使用。

Here's your updated js: 这是您更新的js:

$('.slider').each(function () {
    var $this = $(this);
    $this.cycle({
        fx: 'scrollHorz',
        slides: '> a',
        sync: true,
        progressive: function() {
           var slides = $('.other-slides', $this).html();
           return $.parseJSON( slides );
        },
        speed: 'fast',
        timeout: 0,
        autoHeight: 'container',
        next: $this.next('.slider-navigation').find('.next'),
        prev: $this.next('.slider-navigation').find('.prev'),
        loader: true
    });
});

and html: 和html:

<div class="slider">
    <a href="/">
        <img src="example.jpg">
    </a>
    <script class="other-slides" type="text/cycle">
    [
        "<a href='/'><img src='example2.jpg' /></a>",
        "<a href='/'><img src='example3.jpg' /></a>",
        "<a href='/'><img src='example4.jpg' /></a>"
    ]
    </script>
</div>

And here's a fiddle: http://jsfiddle.net/N7tgL/ 这是一个小提琴: http : //jsfiddle.net/N7tgL/

Thanks guys worked great... however, I did find that 谢谢大家工作很棒...但是,我确实发现了

next: '> .next',
prev: '> .prev',

worked for finding elements with class selector next or prev. 使用下一个或上一个类选择器来查找元素。

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

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