简体   繁体   English

在Cycle2中更改幻灯片时更改CSS

[英]Changing CSS when Slide Changes in Cycle2

http://jsfiddle.net/gS8LE/ http://jsfiddle.net/gS8LE/

The effect that I'm going for is when a slide is active, the corresponding slide button (outside of the slideshow container) changes CSS. 我要获得的效果是当幻灯片处于活动状态时,相应的幻灯片按钮(在幻灯片容器外部)会更改CSS。

I hope this will make the site easier to understand for users; 我希望这将使用户更容易理解该网站; to see a slide and a corresponding change of buttons. 查看幻灯片和相应的按钮更改。

HTML

<!-- BUTTON -->
<a href="#" data-cycle-context=".mySlideshows" 
            data-cycle-cmd="goto" 
            data-cycle-arg="0"> SLIDE 0 
</a>

<!-- SLIDE -->
<div class="mySlideshows" data-cycle-slides="> div">
    <div id="slide-0"> SOME TEXT </div>

CSS
.active { color: red; }

JS
$( '.mySlideshows' ).cycle(

    // Can I change the active CSS rule 
    // here with Cycle's options?

    // When SLIDE 0 is active
    // Change  to have the active class (.active)

);

Using Cycle 2's pager functionality to handle this would be the simplest approach. 使用Cycle 2的寻呼机功能来处理此问题将是最简单的方法。 It's really flexible and shouldn't require you specify the pager template on every slide for something like this. 它确实很灵活,不需要您在每张幻灯片上都指定类似这样的页面模板。 Here's the example html: 这是示例html:

<div class="mySlideshows">
    <div><p>Slide text...</p></div>
    <div><p>Slide text...</p></div>
    <div><p>Slide text...</p></div>
</div>

<ul id="custom-pager"></ul>

the JavaScript: JavaScript:

$('.mySlideshows').cycle({
    slides: '> div',
    pager: '#custom-pager',
    pagerTemplate: '<li><a href="">Slide {{slideNum}}</a></li>',
    pagerActiveClass: 'active'
});

and an updated fiddle: http://jsfiddle.net/gS8LE/5/ 和更新的小提琴: http : //jsfiddle.net/gS8LE/5/

This works well if you just need your pager links to just show the slide number. 如果您只需要寻呼机链接仅显示幻灯片编号,则此方法效果很好。 However it can be easily modified to also show custom text in the pager link for each slide. 但是,可以很容易地对其进行修改,以在每张幻灯片的寻呼机链接中显示自定义文本。 You just need to put a data attribute onto your slides. 您只需要在幻灯片上放置一个数据属性即可。 It can be called anything so long as it starts with 'data-cycle-'. 只要以“ data-cycle-”开头,就可以称为任何东西。 Here's the modified html for that: 这是为此修改的html:

<div class="mySlideshows">
    <div data-cycle-pager-title="Custom link text">
        <p>Slide text...</p>
    </div>
    <div data-cycle-pager-title="Another link">
        <p>Slide text...</p>
    </div>
    <div data-cycle-pager-title="Final link">
        <p>Slide text...</p>
    </div>
</div>

the updated JavaScript: 更新的JavaScript:

$('.mySlideshows').cycle({
    slides: '> div',
    pager: '#custom-pager',
    pagerTemplate: '<li><a href="">{{pagerTitle}}</a></li>',
    pagerActiveClass: 'active'
});

and here's a fiddle for this setup: http://jsfiddle.net/gS8LE/4/ 这是此设置的小提琴: http : //jsfiddle.net/gS8LE/4/

When click on an anchor, you can remove class active from all the anchors inside ul.nav and only add this class active to the clicked anchor: 单击锚点时,可以从ul.nav所有锚点中删除active类,而仅将active类添加到单击的锚点中:

$('ul.nav li a').click(function () {
    $('ul.nav li a').removeClass('active');
    $(this).addClass('active');
});

Updated Fiddle 更新小提琴

Basically (and it's messy) you have to create a pager div and in each slide, add a custom pager template. 基本上(这很麻烦),您必须创建一个寻呼机div,并在每张幻灯片中添加一个自定义寻呼机模板。 Then use the CSS rule to handle the active slide. 然后使用CSS规则处理活动幻灯片。

It's a lot of inline Cycle data attributes. 有很多内联Cycle数据属性。 But it works! 但这有效!

http://jsfiddle.net/gS8LE/3/ http://jsfiddle.net/gS8LE/3/

<div data-cycle-pager-template="<li><a href=# data-cycle-cmd=goto data-cycle-arg=0>SLIDE 1</a> </li>">
<p>Slide Content</p>
</div>

<div id="custom-pager"></div>


    CSS
        .cycle-pager-active {
            background-color: red;
        }

Note: When using data-cycle-pager-template you have to remove all quotations. 注意:使用data-cycle-pager-template ,必须删除所有引号。

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

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