简体   繁体   English

Cycle2-无法初始化滑块

[英]Cycle2 - Can't initialize the slider

Hopefully someone can help me with this. 希望有人可以帮助我。 I am not an expert on use of Cycle2 jQuery plugin and I would like to implement it. 我不是使用Cycle2 jQuery插件的专家,我想实现它。 Supposedly Cycle2 is supposed to be able to slide composites and that is what I am trying to do. 据说Cycle2应该能够滑动复合材料,而这正是我想要做的。

I have html that looks like the sample below. 我有看起来像下面的示例的html。 The code is actually generated by djangocms and I don't have the ability to add attributes to the html structure unless I am adding them using JavaScript... 该代码实际上是由djangocms生成的,除非我使用JavaScript添加属性,否则我无法将属性添加到html结构中...

So I suppose I am left with one option and that is to initialize the slider using this: 所以我想我只有一个选择,那就是使用以下方法初始化滑块:

$('.cycle-slideshow').cycle();

But that doesn't seem to do anything. 但这似乎无济于事。

I also tried this: 我也试过这个:

// Cycle Slideshow
$('.cycle-slideshow').attr('data-cycle-fx', 'scrollHorz');
$('.cycle-slideshow').attr('data-cycle-timeout', '2000');
$('.cycle-slideshow').attr('data-cycle-slides', '> div');

My HTML structure... 我的HTML结构...

<div class="cycle-slideshow">

<div class="slider">    
    <h2>Teaser Title 1</h2>
     <img src="/media/cms_page_media/2014/8/20/Banner1.png" alt="Teaser Title 1">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

<div class="slider">
    <h2>Teaser Title 2</h2>
    <img src="/media/cms_page_media/2014/8/20/Banner2.png" alt="Teaser Title 2">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

<div class="slider">
    <h2>Teaser Title 3</h2>
    <img src="/media/cms_page_media/2014/8/20/Banner3.png" alt="Teaser Title 3">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

<div class="slider">
    <h2>Teaser Title 4</h2>
    <img src="/media/cms_page_media/2014/8/20/Banner4.png" alt="Teaser Title 4">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

</div>

The reason why that might not work is because Cycle 2 Inits all slideshows right after it loads the plugin, so your jQuery won't affect your slideshows, because Cycle 2 has already been initialized. 之所以可能不起作用,是因为Cycle 2在加载插件后立即启动所有幻灯片,因此jQuery不会影响幻灯片,因为Cycle 2已经初始化。

What happens if you add the attributes and THEN init the slideshow: 如果添加属性,然后将幻灯片放回原处,将会发生什么:

$('.cycle-slideshow')
    .data('cycle-fx', 'scrollHorz')
    .data('cycle-timeout', '2000')
    .data('cycle-slides', '> div')
    .cycle();

You can also, try to pass your options as an object to your slideshow: 您也可以尝试将选项作为对象传递给幻灯片:

$('.cycle-slideshow')
    .cycle({
        'fx' : 'scrollHorz',
        'timeout' : 2000,
        'slides' : '> div'
    });

Finally, you also try re-writing the cycle 2 defaults in order for all your slideshows to work accordingly. 最后,您还尝试重新编写第2周期的默认设置,以使所有幻灯片均能正常工作。 This should be avoided unless necessary, but it might work! 除非有必要,否则应避免这种情况,但它可能会起作用!

$.fn.cycle.defaults = $.extend($.fn.cycle.defaults, {
        'fx' : 'scrollHorz',
        'timeout' : 2000,
        'slides' : '> div'
    });

$('.cycle-slideshow').cycle();

UPDATE: 更新:

You might want to re-write the defaults before $(document).ready so that cycle uses your default options when initiating the plugin. 您可能希望在$(document).ready之前重写默认值,以便该周期在启动插件时使用默认选项。 It automatically calls itself on $(document).ready . 它会自动在$(document).ready上调用自己。

Source Code: https://github.com/malsup/cycle2/blob/master/src/jquery.cycle2.core.js#L678-L681 源代码: https : //github.com/malsup/cycle2/blob/master/src/jquery.cycle2.core.js#L678-L681

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

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