简体   繁体   English

在Cycle2中使用jQuery切换基于幻灯片的标题

[英]Toggle Title Based on Slide Using jQuery in Cycle2

Hopefully a simple fix here at the end of a long week. 希望在漫长的一周结束后在这里进行简单的修复。

I'm using Cycle2 for an image slider. 我将Cycle2用于图像滑块。 C2 allows for transitioning captions based on the given slide, but it doesn't seem to allow for transitioning additional elements. C2允许根据给定的幻灯片转换字幕,但似乎不允许转换其他元素。 In this case, I need a title in addition to the caption, but I don't want to use the built-in overlay feature (see fiddle for example of what I mean -- essentially, the title in the red bar needs to change based on the image). 在这种情况下,除了标题外,我还需要一个标题,但是我不想使用内置的叠加功能(请参见小提琴,了解我的意思-本质上,红色栏中的标题需要更改根据图片)。

Here's the fiddle: http://jsfiddle.net/jeremytripp/j58Ky/ 这是小提琴: http : //jsfiddle.net/jeremytripp/j58Ky/

Any thoughts or suggestions? 有什么想法或建议吗?

Here's some HTML so SO will let me post: 这是一些HTML,因此让我发布:

<div id="figuretitle" class="fluid"> <b>&nbsp;Combination Circuit</b> 
</div>
<div>
    <!-- prev/next links -->
    <div>
        <table style="width:100%;">
            <tr>
                <td style="width:50%;"> <a href=# id="prev">Prev</a>

                </td>
                <td align="right" style="width:50%;"> <a href=# id="next">Next</a>

                </td>
            </tr>
        </table>
    </div>
    <!-- slideshow -->
    <div class="cycle-slideshow" data-cycle-fx=scrollHorz data-cycle-timeout=0 data-cycle-prev="#prev" data-cycle-next="#next" data-cycle-swipe=true data-cycle-caption="#adv-custom-caption" data-cycle-caption-template="{{cycleTitle}}">
        <img src="http://jquery.malsup.com/cycle2/images/p1.jpg" data-cycle-title="<b>Figure 6-2</b> A combination circuit with a parallel component and series component.">
        <img src="http://jquery.malsup.com/cycle2/images/p2.jpg" data-cycle-title="<b>Figure 6-3</b> The <i>R</i><sub>1</sub> resistor is in series with an equivalent resistor for <i>R</i><sub>2</sub> and <i>R</i><sub>3</sub>.">
        <img src="http://jquery.malsup.com/cycle2/images/p3.jpg" data-cycle-title="<b>Figure 6-4</b> A circuit can be reduced to a single equivalent resistor.">
    </div>
</div>
<!-- empty element for caption -->
<div id="adv-custom-caption" class="fluid"></div>

JS added to change text in red bar per image selection 添加了JS,以根据图片选择更改红色栏中的文本

            el.html(opts.API.tmpl(template, slideOpts, opts, currSlide));
            $("#figuretitle").html(opts.API.tmpl(template, slideOpts, opts, currSlide));
            el.show();

Demo: http://jsfiddle.net/4Wxcg/ 演示: http//jsfiddle.net/4Wxcg/

Thanks to JHuangweb for getting me on the right track. 感谢JHuangweb让我步入正轨。 For future answer-seekers, here's the full solution. 对于未来的求职者,这里是完整的解决方案。

http://jsfiddle.net/jeremytripp/j58Ky/1/ http://jsfiddle.net/jeremytripp/j58Ky/1/

Basically you have to write in an additional caption selector into the Cycle2 JS and leave the template option blank. 基本上,您必须在Cycle2 JS中编写一个附加的字幕选择器,并将模板选项留为空白。 It's in the section commented as "caption plugin" -- a little more than halfway through the plugin JS. 在该部分中被注释为“字幕插件”-在插件JS的一半以上。 Your new selectors will look like this: 您的新选择器将如下所示:

figureswitch: '> .cycle-switch',
figureswitchTemplate: '{{}}',

Then add the JS as JHuangweb suggested: 然后按照JHuangweb的建议添加JS:

var el = opts.API.getComponent(name);
            if (el.length && template) {
                el.html(opts.API.tmpl(template, slideOpts, opts, currSlide));
                $("#figureswitch").html(opts.API.tmpl(template, slideOpts, opts, currSlide));
                el.show();
            } else {
                el.hide();

Then add these two options in the plugin HTML: 然后在插件HTML中添加以下两个选项

data-cycle-figureswitch="#figureswitch" 
data-cycle-figureswitch-template="{{cycleSwitch}}" 

Call your newly-recognizable ID where you want it: 在您需要的地方调用您新识别的ID:

<div id="figureswitch"></div>

and whip up a bit of CSS for it to follow and voila: 并添加一些CSS以使其遵循,瞧:

#figureswitch {
    height:3%;
    padding-top:1%;
    background-color:#C00;
    color:#FFFFFF;
    font-size: 0.9em;
    font-family: Verdana;
}

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

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