jQuery Cycle-如何根据活动幻灯片更改元素的类 [英] jQuery Cycle - How to change class of element based on active slide

查看:114
本文介绍了jQuery Cycle-如何根据活动幻灯片更改元素的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jQuery Cycle 进行边秀.它的工作方式是单击.next或.prev divs可以按预期方式在幻灯片中循环播放.我要做的是让它更新.timeline_pagination中的UL,并将活动"类添加/删除到相应的幻灯片中.例如.如果#tl_2004幻灯片处于活动状态,则#goto2004 li需要该类处于活动状态.我不希望#goto ***功能正常,它们纯粹是作为显示参考.

I have a sideshow using jQuery Cycle. The way it works is that clicking the .next or .prev divs cycles through the slideshow as expected. What I need it to do though is to have it update the UL in .timeline_pagination and add/remove the class 'active' to the corresponding slide. eg. if the #tl_2004 slide is active, the then #goto2004 li needs the class active. I do not want or need the #goto*** to be functional, they're purely there as a display reference.

谢谢您的帮助.

这是标记:

<!-- FAKE PAGINATION -->    
<div class="timeline_pagination">
        <ul>
            <li class="active" id="goto1994">1994</li>
            <li id="goto2000">2000</li>
            <li id="goto2004">2004</li>
            <li id="goto2007">2007</li>
        </ul>
</div>
<!-- END FAKE PAGINATION --> 

<div class="timeline_slideshow">
    <div class="prev"></div>
    <div class="next"></div>
    <div class="tl_slideshow">

        <div id="tl_1994">
            <div class="caption">
                <h4>title</h4>
                <p>my text</p>
            </div>
        </div>

        <div id="tl_2000">
            <div class="caption">
                <h4>title</h4>
                <p>my text</p>
            </div>
        </div>

        <div id="tl_2004">
            <div class="caption">   
                <h4>title</h4>
                <p>my text</p>
            </div>
        </div>

        <div id="tl_2007">
            <div class="caption">
                <h4>title</h4>
                <p>my text</p>
            </div>
        </div>

    </div>
</div>

这是JavaScript:

Here is the javascript:

$('.tl_slideshow').cycle({
        fx:         'scrollHorz',
        next:       '.next', 
        prev:       '.prev',
        timeout:    0,
        speed:      750,
        nowrap:     1,
        after:      onAfter
});

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('.prev')[index == 0 ? 'fadeOut' : 'fadeIn']();
    $('.next')[index == opts.slideCount - 1 ? 'fadeOut' : 'fadeIn']();
}

推荐答案

尝试使用此jquery代码

Try with this jquery code

var PREFIX_ID_PAGINATION = "goto";
var PREFIX_ID_SLIDESHOW = "tl_";

$('.tl_slideshow').cycle({
        fx:         'scrollHorz',
        next:       '.next',
        prev:       '.prev',
        timeout:    0,
        speed:      750,
        nowrap:     1,
        after:      onAfter
});

function onAfter(prev, current, opts) {
    var index = opts.currSlide;
    $('.prev')[index == 0 ? 'fadeOut' : 'fadeIn']();
    $('.next')[index == opts.slideCount - 1 ? 'fadeOut' : 'fadeIn']();

    var current_id = $(current).attr('id');
    if( current_id.match("^" + PREFIX_ID_SLIDESHOW + "[0-9]+$")){
        $('li.active').removeClass('active');
        $('#' + PREFIX_ID_PAGINATION + current_id.replace(PREFIX_ID_SLIDESHOW,"")).addClass('active');
    }
}

有一个jsfiddle示例此处

There is a jsfiddle example here

这篇关于jQuery Cycle-如何根据活动幻灯片更改元素的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆